springmvc 简单入门

来源:互联网 发布:mac os x10.12镜像下载 编辑:程序博客网 时间:2024/05/21 15:50

各个jar包的版本,以及依赖包版本:

spring-framework-3.0.5.RELEASE

commons-logging-1.1.2-bin.zip  

本示例采取spring自带的入门教程,默认配置,xml配置形式.

首先引入以下jar包到工程:


 工程类结构图:



一.配置web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">  <servlet>    <servlet-name>springmvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>springmvc</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping></web-app>
以上大家会看见了配置了一个Servlet,DispatcherServlet其实就是一个Servlet,它继承自HttpServlet 下面的url-pattern中是/表示过滤所有请求,这个是标准的将EE配置.

二编写Java类,一个简单的跳转,和一个控制台输出.
package org.hc.web.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;public class HelloWorld implements Controller {@Overridepublic ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {System.out.println("org.hc.web.controller.HelloWorld.handleRequest执行.");return new ModelAndView("/helloworld");} }
以上返回值/helloworld代表跳转到指定的文件夹下的${prefix}/helloworld${suffix}资源处,在以下的配置文件会讲解为什么,在这里只是留下一个印象,下面会分析这里写的字符串是什么意思.

三.编写对应springmvc的配置文件.
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"   xmlns:context="http://www.springframework.org/schema/context"   xmlns:p="http://www.springframework.org/schema/p"   xmlns:mvc="http://www.springframework.org/schema/mvc"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">          <!-- controller对应的处理类 --> <bean name="/hello" class="org.hc.web.controller.HelloWorld"></bean><!--  视图解析器--><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp"></property> <property name="suffix" value=".jsp"></property></bean> </beans>  

转载标明出处:http://blog.csdn.net/yhc13429826359/article/details/8757203
  3.1以上代码id="viewResolver"那么这个是什么意思咧?
按照英文直译就是内部的视图解析器.: 
InternalResourceViewResolver属性解释: 
还记得上面提到过的${prefix}/helloworld${suffix}吗,那么下面说说这个什么意思,相信大家通过属性的name属性就知道是什么意思了 name="prefix"指的是需要跳转的视图资源在那个文件夹下面,那么name="suffix"指的是跳转的资源是什么后缀,那么按照上面我们的 HelloWorld类的handleRequest方法的返回的ModelAndView("/helloworld"),那么拼接起来的完整路径是/WEB-INF/jsp/helloworld][.jsp 分析一下前缀(/WEB-INF/jsp)和后缀(.jsp) 是spring中配置的,那么我们的controller中只需要返回什么了,那就是资源名称.

 3.2name="/hello" 是指的什么咧?
   这个指的是浏览器的访问路径,一般的请求是  http://127.0.0.1/工程名/hello
  这个配置的路径就是指的紧接着项目名后的路径名.

注意 :需要说一下的是由于我们配置的前缀是/WEB-INF/jsp,那么你就要在WEB-INF文件夹下创建对应的jsp文件夹下,还有我们的HelloWorld.handleRequest()方法返回的是/helloworld这个就代表的是在前缀配置的根目录下的[(前缀目录位置+controller返回的资源名称+后缀)]组成的路径,如果你返回的是new ModelAndView("/login/helloworld");那么就需要在/WEB-INF/jsp目录下建立login文件夹.然后再将helloword.jsp移至login目录下,否则就是404你懂得.

补充:我们使用的是默认配置,没有配置springmvc的配置文件在什么位置,那么他默认是在查找方式是在WEB-INF/[servlet-name-servlet.xml],也就是你在web.xml中配置的DispatcherServlet的[servlet-name加上-servlet.xml],所以我的文件放置在WEB-INF/目录下并且命名为springmvc-servlet.xml.

源码下载地址http://download.csdn.net/detail/yhc13429826359/5218133

简单示例完.如果有不足地方请指出.

 
  
 
  














原创粉丝点击