Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的Spring MVC框架或集成其他MVC开发框架,如Struts1(现在一般不用),Struts 2(一般老项目使用)等。
Dispatcher Servlet分发器
Handler Mapping 处理器映射
Controller 控制器
ModelAndView
ViewResolver 视图解析器
<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:component-scan base-package="com.java.demo1.controller">context:component-scan>
<mvc:annotation-driven>mvc:annotation-driven>
<bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/">property>
<property name="suffix" value=".jsp">property>
bean>
beans>
<servlet>
<servlet-name>actionservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>actionservlet-name>
<url-pattern>*.actionurl-pattern>
servlet-mapping>
注意:有公司配置为:*.do,shtml,/(所有的资源将被拦击,包括图片等)
@Controller //普通类被用Controller注释就变成了servlet
public class MyController {
@RequestMapping(value="/my.action")
public void test(){
System.out.println("test");
}
}
@RequestMapping(value="/aa")
public class MyController {
@RequestMapping(value ="/my.action")
public void test(){
System.out.println("test");
}
}
@Controller //普通类被用Controller注释就变成了servlet
@Scope("prototype")
public class MyController {
@RequestMapping(value ="/my.action",method=RequestMethod.POST)
public void test(){
System.out.println("test");
}
@RequestMapping(value={"/a.action","b.action","c.action"})
public void test2(){
System.out.println("test");
}
}
@RequestMapping(value ="/register.action",method=RequestMethod.POST)
public void register(String username,String password){
System.out.println(username+","+password);
}
可以自动转换基础数据类型
<form action="register.action" method="post">
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="提交">
</form>
<filter>
<filter-name>charfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>charfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
@RequestMapping(value ="/register1.action",method=RequestMethod.POST)
public void register1(User user){
System.out.println(user.getUsername()+","+user.getPassword());
}
注意:页面中的字段名要和对象的字段同名