现在主流的Web MVC Struts2这个主力框架最近问题重重出现,转而用大家推荐的Spring MVC了,这也是作为一名程序员需要掌握的框架,不过要想灵活运用Spring MVC来应对Web开发,就必须要掌握它的配置及原理。在此对一个示例做一个说明
1.架包导入:
Spring3.*:导入相关的包,例如:spring.jar、spring-webmvc.jar、commons-logging.jar
2.web.xml配置
<servlet>
//配置servlet的名称
<servlet-name>springM</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
//如果不用默认springmvc的配置(默认配置为 [<servlet-name>]-servlet.xml,例如springM-servlet.xml)
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
//过滤拦截,对.do的请求拦截
<servlet-mapping>
<servlet-name>springM</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3.spring-mvc.xml 文件配置
//设置使用注解的扫描的包
<context:component-scan base-package="com.action" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
//完成请求和注解POJO的映射
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="cacheSeconds" value="0" />
//数据转换用到
<property name="webBindingInitializer">
<bean class="com.commons.MyWebBindingInitializer"/>
</property>
//消息内容转换
<property name="messageConverters">
<list>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
</list>
</property>
</bean>
//视图解析 对转向页面的路径解析。prefix:前缀, suffix:后缀,对自动对action中返回的文件加前缀和后缀
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
</bean>
<mvc:default-servlet-handler/>
</beans>
在此做一个说明 Spring会创建一个WebApplicationContext上下文,称为父上下文(父容器)
DispatcherServlet是一个Servlet,可以同时配置多个,每个 DispatcherServlet有一个自己的上下文对象(WebApplicationContext),称为子上下文(子容器),子上下文可以访问父上下文中的内容,但父上下文不能访问子上下文中的内容
4.applicationContext.xml配置
这个配置文件可以配置数据访问,事务等相关配置,在此暂略
5.访问详解
分三种访问形式
1》通过配置方法设置访问
@Controller
public class Controller {
数据参数可以有多种形式传递,例如http://localhost:8089/test/test.do?name=hhh,这个是按参数传递,其中对RequestParam是必须的参数,否则会报错
@RequestMapping("test/test.do")
public String testLog(@RequestParam("name") String name,String pwd,HttpServletRequest request){}
参数也可以采用对象的形式 例如http://localhost:8089/test/test.do?name=hhh,
public String testLog( Person p){},那么自动对给person.name赋值
}
2>指定一个action,通过参数配置访问哪个方法
@Controller
@RequestMapping("/test2/login.do")//指定访问的action
public class Controller2 {
@RequestMapping
public String log1(String name,String pwd) 默认为调用该方法
@RequestMapping(params = "method=1",method = RequestMethod.GET) 通过参数method=1来区分调用该方法
public String log2(String name,String pwd){}
}
3>类mapping + 子mapping配置实现
@Controller
@RequestMapping("/test3/*") 类请求url
public class Controller3 {
@RequestMapping("log3.do")//方法url
public String log(String name,String pwd){}
}
4>窄化处理映射
@Controller
@RequestMapping(value="/test3") 类请求通用配置
public class Controller4 {
@RequestMapping(value="log4.do")//方法url对类请求窄化,在此访问的url : http://localhost:8089/test3/log4.do
public String log(String name,String pwd){}
}
对于方法返回的数据有几种情况
1.字符串 对于字符串,那么在前面的配置中加上配置的前缀+ 字符串+后缀
2.new ModelAndView("login1"); 返回值的形式跟1一致,唯一的区别是可以带参数
3.ModelAndView("redirect:login1");new ModelAndView(new RedirectView("login1"));