1 前言
在史上最简单的 Spring MVC 教程(二)中,咱们讲解了常见的处理器映射(handlerMapping),并给出了应用示例。在本篇博客中,咱们讲解常见的控制器(Controller),在这里有一点需要大家知晓,那就是:在我们创建Controller,并继承父类的时候,父类上会被画上一条横线,这表示该类已经过时啦!这是因为在 spring 框架在升级到 Spring 3.0 后,推荐大家使用注解的方式进行开发,因此用继承的方式就过时啦!
2 Controller
2.1 CommandController
在使用命令控制器 CommandController 的时候,我们需要在类的构造方法中进行传参,指定 command 对应的实体。 由于要指定实体,我们就需要创建一个实体类,在此以 Person 为例;其次,我们需要在“springmvc-servlet.xml”中声明一个命令控制器;最后,在访问链接的地址有多个单词的时候,都需要小写,也可以去掉其中的 Controller 单词。修改后的项目结构图如下所示:
![项目结构图](https://img-blog.csdn.net/20170125124016186?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzUyNDY2MjA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
第一步:创建实体类(Person)
package spring.mvc.domain;
/**
* Created by 维C果糖 on 2017/1/25.
*/
public class Person {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
第二步:创建命令控制器(DemoCommandController)
package spring.mvc.controller;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;
import spring.mvc.domain.Person;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Created by 维C果糖 on 2017/1/25.
*/
public class DemoCommandController extends AbstractCommandController {
public DemoCommandController() {
this.setCommandClass(Person.class);
}
@Override
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response,
Object command, BindException e) throws Exception {
Person p = (Person) command;
System.out.println(p);
return null;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
第三步:修改“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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="order" value="1"/>
</bean>
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/a.aciton">homeController</prop>
<prop key="/b.aciton">homeController</prop>
<prop key="/hello.aciton">homeController</prop>
</props>
</property>
<property name="order" value="0"/>
</bean>
<bean id="controllerClassNameHandlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="homeController" name="/hello.action" class="spring.mvc.controller.CeshiController" />
<bean class="spring.mvc.controller.DemoCommandController" />
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
在使用简单表单控制器 SimpleFormController 的时候,我们也需要在类的构造方法中进行传参,指定 command 对应的实体。 接下来,同 CommandController 的配置类似,需要在“springmvc-servlet.xml”中声明一个简单表单控制器。由于这是表单控制器,所以我们需要新建一个 JSP 页面用于提交表单。在这里,有一点需要注意:当链接以 get 方式提交时,转向 formView 视图;当链接以 post 方式提交时,转向 successView 视图。修改后的项目结构图如下所示:
![项目结构图](https://img-blog.csdn.net/20170125142415313?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzUyNDY2MjA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
第一步:创建简单表单控制器(PersonFormController)
package spring.mvc.controller;
import org.springframework.web.servlet.mvc.SimpleFormController;
import spring.mvc.domain.Person;
/**
* Created by 维C果糖 on 2017/1/25.
*/
public class PersonFormController extends SimpleFormController {
public PersonFormController() {
this.setCommandClass(Person.class);
}
protected void doSubmitAction(Object command) throws Exception {
Person p = (Person) command;
System.out.println(p);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
第二步:新建 JSP 页面(PersonForm)
<%--
Created by IntelliJ IDEA.
User: 维C果糖
Date: 2017/1/25
Time: 14:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>人员列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/personform.action" method="post">
<table>
<tr>
<td>编号:</td>
<td><input tyep="text" name="id"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input tyep="text" name="name"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input tyep="text" name="age"></td>
</tr>
<tr>
<td colspan="2"><input type="button" name="btnOK" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
第三步:修改“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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="order" value="1"/>
</bean>
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/a.aciton">homeController</prop>
<prop key="/b.aciton">homeController</prop>
<prop key="/hello.aciton">homeController</prop>
</props>
</property>
<property name="order" value="0"/>
</bean>
<bean id="controllerClassNameHandlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="homeController" name="/hello.action" class="spring.mvc.controller.CeshiController" />
<bean class="spring.mvc.controller.DemoCommandController" />
<bean class="spring.mvc.controller.PersonFormController" >
<property name="formView" value="PersonForm"/>
<property name="successView" value="index"/>
</bean>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
2.3 WizardController
在使用向导控制器WizarController 的时候,我们也需要在类的构造方法中设置关联的实体,并且可以在构造方法中实现页面参数的回显功能。 接下来,在类中覆写 processFinish 方法,实现点击“完成”操作后需要跳转的页面;覆写 processCancel 方法,实现点击“取消”操作后跳转的页面。由于在使用向导控制器时,涉及多个页面,因此我们需要新建多个 JSP 页面,并在“springmvc-servlet.xml”中声明一个向导控制器,并配置页面的流转顺序。修改后的项目结构图如下所示:
![项目结构图](https://img-blog.csdn.net/20170126013026189?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzUyNDY2MjA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
第一步:创建向导控制器(DemoWizardController)
package spring.mvc.controller;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractWizardFormController;
import spring.mvc.domain.Person;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Created by 维C果糖 on 2017/1/26.
*/
public class DemoWizardController extends AbstractWizardFormController{
public DemoWizardController() {
this.setCommandClass(Person.class);
this.setCommandName("p");
}
@Override
protected ModelAndView processFinish(HttpServletRequest request,
HttpServletResponse response, Object command, BindException e) throws Exception {
Person p = (Person) command;
System.out.println(p);
return new ModelAndView("index");
}
@Override
protected ModelAndView processCancel(HttpServletRequest request,
HttpServletResponse response, Object command, BindException e) throws Exception {
return new ModelAndView("wizard/PersonInfo");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
第二步:新建 JSP 页面(PersonInfo、PersonEdu、PersonWork)
<%--
Created by IntelliJ IDEA.
User: 维C果糖
Date: 2017/1/26
Time: 00:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>人员信息 PersonInfo</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/demowizard.action" method="post">
<table>
<tr>
<td>编号:</td>
<td><input tyep="text" name="id" value="${p.id}"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="_target1" value="下一步"/>
<input type="submit" name="_cancel" value="取消"/>
</td>
</tr>
</table>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
<%--
Created by IntelliJ IDEA.
User: 维C果糖
Date: 2017/1/26
Time: 00:18
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>教育信息 PersonEdu</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/demowizard.action" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input tyep="text" name="id" value="${p.name}"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="_target0" value="上一步"/>
<input type="submit" name="_cancel" value="取消"/>
<input type="submit" name="_target2" value="下一步"/>
</td>
</tr>
</table>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
<%--
Created by IntelliJ IDEA.
User: 维C果糖
Date: 2017/1/26
Time: 00:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>工作信息 PersonWork</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/demowizard.action" method="post">
<table>
<tr>
<td>年龄:</td>
<td><input tyep="text" name="id" value="${p.age}"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="_target1" value="上一步"/>
<input type="submit" name="_cancel" value="取消"/>
<input type="submit" name="_finish" value="完成"/>
</td>
</tr>
</table>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
第三步:修改“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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="order" value="1"/>
</bean>
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/a.aciton">homeController</prop>
<prop key="/b.aciton">homeController</prop>
<prop key="/hello.aciton">homeController</prop>
</props>
</property>
<property name="order" value="0"/>
</bean>
<bean id="controllerClassNameHandlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="homeController" name="/hello.action" class="spring.mvc.controller.CeshiController" />
<bean class="spring.mvc.controller.DemoCommandController" />
<bean class="spring.mvc.controller.PersonFormController" >
<property name="formView" value="PersonForm"/>
<property name="successView" value="index"/>
</bean>
<bean class="spring.mvc.controller.DemoWizardController" >
<property name="pages">
<list>
<value>wizard/PersonInfo</value>
<value>wizard/PersonEdu</value>
<value>wizard/PersonWork</value>
</list>
</property>
</bean>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
在完成以上操作及配置后,启动 tomcat 服务器,在 Chrome 浏览器中访问 http://localhost:8080/springmvc/demowizard.action,然后在显示的页面中,依次输入编号、姓名和年龄,点击“完成”,将会返回如下页面:
![操作结果页面](https://img-blog.csdn.net/20170126013558488?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzUyNDY2MjA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
———— ☆☆☆ —— 返回 -> 史上最简单的 Spring MVC 教程 <- 目录 —— ☆☆☆ ————