springMVC第一天
springMVC工作原理
1.客户端请求提交到DispatcherServlet;
2.由DispatcherServlet控制器查询一个或多个HandlerMapping,找到处理请求的Controller;
3.DispatcherServlet将请求提交到Controller;
4.Controller调用业务逻辑处理后,返回ModelAndView;
5.DispatcherServlet查询一个或多个ViewResoler视图解析器,找到ModelAndView指定的视图;
6.视图负责将结果显示到客户端。
作者:爱总结的小仙女
链接:https://www.imooc.com/article/17468
来源:慕课网
搭建SpringMVC开发环境
1、springMVC结构
1.1DispatcherServlet:中央处理器
1.2Controller:类似于struts2的action
1.3handlerMappping:映射处理器
1.4ModelAndView:
1.5viewResolver
1.6interceptor:拦截器
2、搭建环境
2.1配置web.xml对dispatcherServlet做配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<!--不能使用/* 使用*.do-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2.2建立TestController
public class TestController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
//ModelAndView中的viewName指的是视图解析器前缀和后缀之间的路径
return new ModelAndView("index");
}
}
2.3对controller进行配置(文件名是web.xml中的名字-servlet.xml)配置springMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!--默认的使用BeanNameHandlerMapping这个映射处理器-->
<bean name="/hello.do" class="controller.TestController"></bean>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀是所有jsp的公共路径-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀是所有视图的后缀名-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
springMVC的处理器
1、简单映射处理器(可以把bean和映射进行完全的分离)
此时如果还想使用默认映射处理器就要把它定义出来
<!--默认的使用BeanNameHandlerMapping-->
<bean id="testController" class="controller.TestController"></bean>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀是所有jsp的公共路径-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀是所有视图的后缀名-->
<property name="suffix" value=".jsp"/>
</bean>
<!--配置简单映射处理器这个时候配置的上面的bean就可以省去name
好处是 bean只处理bean不处理映射 但是id要配置
-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!--key是要访问的路径,内容是bean的id-->
<prop key="/hello.do">testController</prop>
</props>
</property>
</bean>
2.参数控制器
<!--通过controller做单独页面跳转的控制器当访问/toIndex.do的时候会跳转到index页面
不经过congtroller寻找方式是通过前缀和后缀进行寻找的-->
<bean name="/toIndex.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="index"/>
</bean>
3、抽象命令控制器(这个控制器是用来做对象封装的)
controller
public class CommandController extends AbstractCommandController{
@Override
protected ModelAndView handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, BindException e) throws Exception {
System.out.println(o);
return new ModelAndView("index");
}
}
model
public class Person {
private String userName;
private String age;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
配置文件
<!--定义抽象命令控制器,必须引入web.servlet的包-->
<bean name="/comm.do" class="controller.CommandController">
<!--用来指定接收的参数类型-->
<property name="commandClass" value="model.Person"/>
</bean>
4、简单表单控制器
要使用这个类同样需要引入org.springframework.web.servlet-3.0.2.RELEASE.jar这个包
model类
public class Person {
private String userName;
private String age;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"userName='" + userName + '\'' +
", age='" + age + '\'' +
", birthday=" + birthday +
'}';
}
}
controller
public class FormController extends SimpleFormController {
@Override
protected void doSubmitAction(Object command) throws Exception {
System.out.println(command);
super.doSubmitAction(command);
}
//自定义日期
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
super.initBinder(request, binder);
}
}
配置文件
当访问/form.do的时候首先会把commandClas指定为model.Person
然后会进入form.jsp form.jsp会提交到FormController 它处理完之后会转发到成功页面
<!--定义简单命令控制器-->
<bean name="/form.do" class="controller.FormController">
<!--用来指定接收的参数类型-->
<property name="commandClass" value="model.Person"/>
<!--依赖于视图解析器-->
<property name="formView" value="form"/>
<property name="successView" value="success"/>
</bean>
for.jsp
这儿需要特别注意的的是action="form.do" 不是action="/form.do"
而且method必须是post方式
<form action="form.do" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>生日:</td>
<td><input type="text" name="birthday"></td>
</tr>
</table>
<input type="submit" value="提交">
</form>
5、控制器的配置文件(第一天)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!--默认的使用BeanNameHandlerMapping-->
<bean id="testController" name="/hello.do" class="controller.TestController"></bean>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀是所有jsp的公共路径-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀是所有视图的后缀名-->
<property name="suffix" value=".jsp"/>
</bean>
<!--如果想使用BeanNameUrlHandlerMapping必须把它显示的定义出来不然会被覆盖掉-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!--配置简单映射处理器这个时候配置的上面的bean就可以省去name
好处是 bean只处理bean不处理映射 但是id要配置
-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!--key是要访问的路径,内容是bean的id-->
<prop key="/hello1.do">testController</prop>
</props>
</property>
</bean>
<!--通过controller做单独页面跳转的控制器-->
<bean name="/toIndex.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="index"/>
</bean>
<!--定义抽象命令控制器,必须引入web.servlet的包-->
<bean name="/comm.do" class="controller.CommandController">
<!--用来指定接收的参数类型-->
<property name="commandClass" value="model.Person"/>
</bean>
<!--定义简单命令控制器-->
<bean name="/form.do" class="controller.FormController">
<!--用来指定接收的参数类型-->
<property name="commandClass" value="model.Person"/>
<property name="formView" value="form"/>
<property name="successView" value="success"/>
</bean>
</beans>
springMVC注解开发
TestController
在controller必须加controller的注解才能被注解扫描器扫描到
@RequestMapping还可以加在类上相当于命名空间
加在类上用于区分相同方法名的不同类中的方法
@Controller
public class TestController {
/*
* String 代表modelAndView中的viewName,前缀和后缀之间的路径它还是依赖于视图解析器
*RequestMapping:指定方法和类的映射,下面是方法的映射
* */
@RequestMapping("/hello.do")
public String hello() {
return "hello";
}
}
配置文件
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀是所有jsp的公共路径-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀是所有视图的后缀名-->
<property name="suffix" value=".jsp"/>
</bean>
<!--配置扫描器 有了扫描器就不需要配置注解驱动
切记对于controller层必须加controller的注解
-->
<context:component-scan base-package="mvc.controller"/>
<!--扫描器可以扫描到的注解有以下几种
@Controller controller层
@Service service层
@Repository dao层
@Component(用于无法分层的类 但是也想使用spring的容器进行管理)
@Autowired:类与类之间的关联
@Resources:类与类之间的关联
-->