springMVC学习笔记三(controller控制器)

========================controller控制器=======================
contorller控制器的任务:
1 收集 验证参数并绑定到命令对象
2 将命令对象交给业务对象,由业务对象返回模型数据
3 返回modelAndView


两种实现方式:
基于实现接口的方式
基于注解的方式






接口的原型为:


public interface Controller {


/**
* Process the request and return a ModelAndView object which the DispatcherServlet
* will render. A {@code null} return value is not an error: It indicates that
* this object completed request processing itself, thus there is no ModelAndView
* to render.
* @param request current HTTP request
* @param response current HTTP response
* @return a ModelAndView to render, or {@code null} if handled directly
* @throws Exception in case of errors
*/
//处理请求
ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;


}


 
--------------------WebContentGenerator: 提供浏览器缓存控制--------------  
------------abstractController(所有controller的父类)------------------
实现 Controller,并继承了 WebContentGenerator
函数原型:
public abstract class AbstractController extends WebContentGenerator implements Controller


改写 helloWorld控制器


public class AbsHelloWorldController extends AbstractController {


@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
//ModelAndView mv = new ModelAndView();
//mv.addObject("message", "hello world!!!!!");
//mv.setViewName("hello");
//return mv;
//可以通过控制器直接写
response.getWriter().write("hello world");
return null;
}
}


在配置文件中注入bean


<bean id="hello" name="/hello" class="cn.yue.mvc.controller.AbsHelloWorldController">
<!--定义请求方法(只允许post请求,进入该控制器时一定要有session存在)-->
<property name="supportedMethods" value="POST"></property>
<!--缓存5秒,不缓存设为0-->
<property name="cacheSeconds" value="5"/>
</bean>


last-modified缓存机制
可以使用以下方式强制缓在过期:
1 ctrl+f5
2 在请求的url后面加上时间戳 
可以通过控制器实现lastModified接口的getLastModified方法修改最新时戳






ETag缓存机制,检查etag标签是否发生变化来确定状态(只过滤DispatcherServlet)
<filter> 
  <filter-name>etagFilter</filter-name> 
  <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class> 
</filter> 
<filter-mapping> 
  <filter-name>etagFilter</filter-name> 
  <servlet-name>example</servlet-name> 
</filter-mapping>




------------------------1 servletForwardingController--------------------
示例代码如下:


public class ForwardingServlet extends HttpServlet {


private static final long serialVersionUID = 1L;


@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("controller forward to servlet");
}


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}


}
 
在web.xml中添加如下内容


<servlet>
<servlet-name>forwarding</servlet-name>
<servlet-class>cn.yue.mvc.servlet.ForwardingServlet</servlet-class>
</servlet>






在SpringMVC-servlet.xml中添加如下内容
<!-- 跳转到指定的servlet -->
<bean name="/forwardToServlet"
class="org.springframework.web.servlet.mvc.ServletForwardingController">
<property name="servletName" value="forwarding" />
</bean>




----------------------2 baseCommandController-------------------
功能:
1 数据绑定
2 验证功能:


baseCommandController子类abstractCommandController
示例代码如下:


创建userModel(命令类对象)
public class UserModel {
/* 用户名 */
private String userName;
/* 密码 */
private String password;


public String getUserName() {
return userName;
}


public void setUserName(String userName) {
this.userName = userName;
}


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


}






控制器代码如下:


public class MyAbstractCommandController extends AbstractCommandController {


/**
* 构造器
*/
public MyAbstractCommandController() {
// 设置命令对象实现类
setCommandClass(UserModel.class);
}


@Override
protected ModelAndView handle(HttpServletRequest req, HttpServletResponse resp, Object command, BindException errors) throws Exception {
// 将命令转换为实际类型
UserModel user = (UserModel) command;
ModelAndView mv = new ModelAndView();
mv.setViewName("abstractCommand");
mv.addObject("user", user);
return null;
}
}






在SpringMVC-servlet.xml中配置处理器


 
<bean name="/abstractCommand" class="cn.yue.mvc.controller.MyAbstractCommandController">
<!-- 也可以通过依赖注入 注入命令实现类 -->
<!-- property name="commandClass" value="cn.yue.mvc.model.UserModel"/ -->
</bean>








abstractCommand.jsp内容如下:
<%@ page language="java" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>abstractCommand</title>
</head>
<body>${user.username}-${user.password}
</body>
</html>
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


baseCommandController子类abstractFormController
支持带步骤的表单提交命令控制器基类,功能:
1 定义表单处理,从控制器获取命令表单
2 提交表单处理,将命令数据绑定到命令对象,验证表单内容,处理命令对象




abstractFormController子类simpleFormController
1 准备要展示的数据
2 提交数据进行处理


示例代码如下:


用户注册控制器


public class RegisterSimpleFormController extends SimpleFormController {


/**
* 构造器
*/
public RegisterSimpleFormController() {
setCommandClass(UserModel.class);
setCommandName("user");
}


/**
* 展示表单时的表单数据
*/
@Override
protected Object formBackingObject(HttpServletRequest request) throws Exception {
UserModel user = new UserModel();
user.setUsername("请输入用户名");
return user;
}


/**
* 展示表单时需要的一些其他数据
*/
@Override
protected Map referenceData(HttpServletRequest request) throws Exception {
Map map = new HashMap();
map.put("cityList", Arrays.asList("山东", "北京", "上海"));
return map;
}


@Override
protected void doSubmitAction(Object command) throws Exception {
UserModel user = new UserModel();
// 调用业务对象进行处理
System.out.println(user);
}


}


在springMVC-servlet.xml中配置处理器
<!-- 用户注册 -->
<bean name="/simpleForm" class="cn.yue.mvc.controller.RegisterSimpleFormController">
<property name="formView" value="register" />
<property name="successView" value="redirect:/success" />
</bean>
<bean name="/success" class="cn.yue.mvc.controller.SuccessController" />




视图页面register.jsp


<%@ page language="java" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>register</title>
<!-- register.jsp -->
</head>
<body>




<form method="post">
username:<input type="text" name="username" value="${user.username}"><br />
password:<input type="password" name="username"><br /> city:
<select>
<c:forEach items="${cityList}" var="city">
<option>${city}</option>
</c:forEach>
</select><br /> <input type="submit" value="注册" />
</form>
</body>
</html>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
simpleFormController子类cancellableFormController取消表单功能


示例代码如下:
控制器CancancelRegisterSimpleFormController 代码如下:


public class CancancelRegisterSimpleFormController extends CancellableFormController {


/**
* 构造器
*/
public CancancelRegisterSimpleFormController() {
setCommandClass(UserModel.class);
setCommandName("user");
}


/**
* 展示表单时的表单数据
*/
@Override
protected Object formBackingObject(HttpServletRequest request) throws Exception {
UserModel user = new UserModel();
user.setUsername("请输入用户名");
return user;
}


/**
* 展示表单时需要的一些其他数据
*/
@Override
protected Map referenceData(HttpServletRequest request) throws Exception {
Map map = new HashMap();
map.put("cityList", Arrays.asList("山东", "北京", "上海"));
return map;
}


/**
* 提交到业务处理
*/
@Override
protected void doSubmitAction(Object command) throws Exception {
UserModel user = new UserModel();
// 调用业务对象进行处理
System.out.println(user);
}


/**
* 取消表单提交
*/
@Override
protected ModelAndView onCancel(Object command) throws Exception {
UserModel user = new UserModel();
// 调用业务对象处理
System.out.println(user);


return super.onCancel(command);
}


}


控制器CancelController内容如下:


public class CancelController implements Controller {


@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setCharacterEncoding("utf-8");
response.getWriter().write("取消表单提交!");
return null;
}
}




springMVC-servlet.xml文件添加


<!-- 取消用户注册 -->
<bean name="/canCancelForm"
class="cn.yue.mvc.controller.CancancelRegisterSimpleFormController">
<property name="formView" value="register" />
<property name="successView" value="redirect:/success" />
<property name="cancelView" value="redirect:/cancel" />
</bean>
<bean name="/cancel" class="cn.yue.mvc.controller.CancelController" />








register.jsp页面内容如下:
<%@ page language="java" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>register</title>
<!-- register.jsp -->
</head>
<body>




<form method="post">
username:<input type="text" name="username" value="${user.username}"><br />
password:<input type="password" name="username"><br /> city:
<select>


<c:forEach items="${cityList }" var="city">
<option>${city }</option>
</c:forEach>
</select><br /> 
<input type="submit" value="注册" />
<input type="submit" name="_cancel" value="取消" />
</form>
</body>
</html>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
abstractFormController子类abstractWizardFormController
多步骤向导表单的支持,如完善个人信息(基本信息,学校信息,工作信息)
通过abstractFormController中的sesionForm,如果值为true,表单信息则存放在session中


示例代码如下:
//学校信息
public class SchoolInfoModel {
/* 学校类型 */
private String schoolType;
/* 学校名称 */
private String schoolName;
/* 专业 */
private String specialty;


public String getSchoolType() {
return schoolType;
}


public void setSchoolType(String schoolType) {
this.schoolType = schoolType;
}


public String getSchoolName() {
return schoolName;
}


public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}


public String getSpecialty() {
return specialty;
}


public void setSpecialty(String specialty) {
this.specialty = specialty;
}
}


//工作信息
public class WorkInfoModel {
/* 城市 */
private String city;
/* 职位 */
private String job;
/* 工作年限 */
private String year;


public String getCity() {
return city;
}


public void setCity(String city) {
this.city = city;
}


public String getJob() {
return job;
}


public void setJob(String job) {
this.job = job;
}


public String getYear() {
return year;
}


public void setYear(String year) {
this.year = year;
}


}


//用户基本信息,修改userModel,添加以下字段和get/set方法
/* 真实姓名 */
private String realname;
/* 学校信息 */
private WorkInfoModel workInfo;
/* 工作信息 */
private SchoolInfoModel schoolInfo;




控制器InfoFillWizardFormController 内容如下:


public class InfoFillWizardFormController extends AbstractWizardFormController {


/**
* 构造器
*/
public InfoFillWizardFormController() {
setCommandClass(UserModel.class);
setCommandName("user");
}


/**
* 数据源
*/
@Override
protected Map referenceData(HttpServletRequest request, int page) throws Exception {
Map map = new HashMap();
if (page == 1) {// 添写学校信息
map.put("schoolTypeList", Arrays.asList("初中", "高中", "大学"));
}
if (page == 2) {// 如果是工作信息
map.put("cityList", Arrays.asList("淄博", "北京", "上海"));
}
return map;
}


/**
* 验证每一页数据的处理方法
*/
@Override
protected void validatePage(Object command, Errors errors, int page) {
}


/**
* 每一页完成时的处理方法
*/
@Override
protected void postProcessPage(HttpServletRequest request, Object command, Errors errors, int page) throws Exception {
}


/**
* 填写成功后的处理方法
*/
@Override
protected ModelAndView processFinish(HttpServletRequest req, HttpServletResponse resp, Object command, BindException errors) throws Exception {
System.out.println(command);
// 重定向到成功页面
return new ModelAndView("redirect:/success");
}


/**
* 取消填写后的处理方法
*/
@Override
protected ModelAndView processCancel(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
System.out.println(command);
return new ModelAndView("redirect:/cancel");
}
}




配置springMVC-servlet.xml文件,添加如下内容:
<!-- 完善个人信息 -->
<bean name="/infoFillWizard" class="cn.yue.mvc.controller.InfoFillWizardFormController">
<!-- 每个步骤的逻辑视图名 -->
<property name="pages">
<list>
<value>wizard/baseInfo</value>
<value>wizard/schoolInfo</value>
<value>wizard/workInfo</value>
</list>
</property>
</bean>






页面信息如下:


jsp/wizard/baseInfo.jsp文件内容如下:


<%@ page language="java" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>register</title>
<!-- register.jsp -->
</head>
<body>
<form method="post">
真实姓名:<input type="text" name="realname" value="${user.realname}"><br />
<input type="submit" name="_target1" value="下一步" />
</form>
</body>
</html>


jsp/wizard/schoolInfo.jsp文件内容如下:
<%@ page language="java" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>register</title>
<!-- register.jsp -->
</head>
<body>
<form method="post">
学校类型:<select name="schoolInfo.schoolType">
<c:forEach items="${schoolTypeList }" var="schoolType">
<option value="${schoolType }"
<c:if test="${user.schoolInfo.schoolType eq schoolType}"> 
  selected="selected" 
</c:if>>
${schoolType}</option>
</c:forEach>
</select><br />
学校名称:<input type="text" name="schoolInfo.schoolName" value="${user.schoolInfo.schoolName}" /><br /> 
专业:<input type="text" name="schoolInfo.specialty" value="${user.schoolInfo.specialty}" /><br />
 <input type="submit" name="_target0" value="上一步" />
  <input type="submit" name="_target2" value="下一步" />
</form>
</body>
</html>


jsp/wizard/workInfo.jsp文件内容如下:


<%@ page language="java" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>register</title>
<!-- register.jsp -->
</head>
<body>
<form method="post">
所在城市:<select name="workInfo.city">
<c:forEach items="${cityList }" var="city">
<option value="${city }"
<c:if test="${user.workInfo.city eq city}">selected="selected"</c:if>>
${city}</option>
</c:forEach>
</select><br />
职位:<input type="text" name="workInfo.job" value="${user.workInfo.job}" /><br /> 
工作年限:<input type="text" name="workInfo.year" value="${user.workInfo.year}" /><br />
<input type="submit" name="_target1" value="上一步" /> 
<input type="submit" name="_finish" value="完成" /> 
<input type="submit" name="_cancel" value="取消" />
</form>
</body>
</html>




---------------------3 parameterizableViewController----------------
参数化视图控制器,不能进行功能处理,根据参数的逻辑视图名直接选择要展示的社图


<bean name="/parameterizableView" 
class="org.springframework.web.servlet.mvc.ParameterizableViewController"> 
<property name="viewName" value="success"/> 
</bean>


---------------------4 abstractUrlViewController-------------------
根据请求url路径直接转化为逻辑视图名的支持基类


urlDecode 是否进行url解码
UrlPathHelper 解析请求路径的工具类


abstractUrlViewController子类urlFilenameViewController
将请求的url路径转换为逻辑视图名并返回转换控制器
<bean name="/index1/*" 
class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/> 
<bean name="/index2/**" 
class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/> 
<bean name="/*.html" 
class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/> 
<bean name="/index3/*.html" 
class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>


此控制器还有两个属性:
prefix:生成逻辑视图名的前缀; 
suffix:生成逻辑视图名的后缀; 


---------------------5 multiActionController-----------------------
一个控制器实现多个处理方法


delegate:功能处理的委托对象


methodNameResolver(方法解析器)有以下几个子类:
AbstractUrlMethodNameResolver,
InternalPathMethodNameResolver,
ParameterMethodNameResolver,
PropertiesMethodNameResolver


requestToViewNameTranslator


示例代码如下:


控制器userController


public class UserController extends MultiActionController {
/* 用户服务类 */
private IUserService userService;
/* 逻辑视图通过依赖方式注入 */
private String creatView;
private String updateView;
private String deleteView;
private String listView;
private String redirectToListView;


/**
* 创建用户

* @param request
* @param response
* @param user
* @return
* @throws Exception
*/
public String create(HttpServletRequest request, HttpServletResponse response, UserModel user) throws Exception {
// 如果是get请求转向新增页面
if ("GET".equals(request.getMethod())) {
return getCreatView();
}
// 添加用户
userService.insert(user);
// 重定向到列表页面
return getRedirectToListView();
}


/**
* 更新用户

* @param request
* @param response
* @param user
* @return
*/
public ModelAndView update(HttpServletRequest request, HttpServletResponse response, UserModel user) {
if ("GET".equals(request.getMethod())) {
ModelAndView mv = new ModelAndView();
// 查询要更新的数据
mv.addObject("command", userService.selectByUserName(user.getUsername()));
mv.setViewName(getUpdateView());
return mv;
}
// 更新用户信息
userService.update(user);
// 重定向到列表页面
return new ModelAndView(getRedirectToListView());
}


/**
* 删除用户

* @param request
* @param response
* @param user
* @return
*/
public ModelAndView delete(HttpServletRequest request, HttpServletResponse response, UserModel user) {
// 如果是get请求转向删除页面
if ("GET".equals(request.getMethod())) {
ModelAndView mv = new ModelAndView();
// 查询要删除的数据
mv.addObject("command", userService.selectByUserName(user.getUsername()));
mv.setViewName(getDeleteView());
return mv;
}
// 删除用户
userService.delete(user.getId());
// 重定向到用户列有页面
return new ModelAndView(getRedirectToListView());
}


/**
* 列表显示所有用户信息

* @param request
* @param response
* @return
*/
public ModelAndView list(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView();
mv.addObject("userList", userService.getAll());
mv.setViewName(getListView());
return mv;
}


/**
* 如果使用委托方式,命令对象的名称只能是command
*/
@Override
protected String getCommandName(Object command) {
return "command";
}


public IUserService getUserService() {
return userService;
}


public void setUserService(IUserService userService) {
this.userService = userService;
}


public String getCreatView() {
return creatView;
}


public void setCreatView(String creatView) {
this.creatView = creatView;
}


public String getUpdateView() {
return updateView;
}


public void setUpdateView(String updateView) {
this.updateView = updateView;
}


public String getDeleteView() {
return deleteView;
}


public void setDeleteView(String deleteView) {
this.deleteView = deleteView;
}


public String getListView() {
return listView;
}


public void setListView(String listView) {
this.listView = listView;
}


public String getRedirectToListView() {
return redirectToListView;
}


public void setRedirectToListView(String redirectToListView) {
this.redirectToListView = redirectToListView;
}


}


springMVC-servlet.xml添加如下内容:
<!-- 用户的crud操作 -->
<bean id="userService" class="cn.yue.mvc.service.IUserService" />
<bean name="/user/**" class="cn.yue.mvc.controller.UserController">
<property name="userService" ref="userService" />
<property name="createView" value="user/create" />
<property name="updateView" value="user/update" />
<property name="deleteView" value="user/delete" />
<property name="listView" value="user/list" />
<property name="redirectToListView" value="redirect:/user/list" />
<!-- 使用PropertiesMethodNameResolver来解析功能处理方法名 -->
<!--property name="methodNameResolver" ref="propertiesMethodNameResolver"/ -->
</bean>




页面视图:
jsp/user/list.jsp页
<%@ page language="java" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>list</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/user/create">用户新增</a>
<br />
<table border="1" width="50%">
<tr>
<th>用户id</th>
<th>用户名</th>
<th>真实姓名</th>
<th>操作</th>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id }</td>
<td>${user.username }</td>
<td>${user.realname }</td>
<td><a
href="${pageContext.request.contextPath}/user/update?username=${user.username}">更新</a>
| <a
href="${pageContext.request.contextPath}/user/delete?username=${user.username}">删除</a>
</td>
</tr>
</c:forEach>
</table>




</body>
</html>




create.jsp页面内容如下:
<%@ page language="java" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>create</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/create" method="post">
用户名: <input type="text" name="username" value="${command.username}" /><br />
真实姓名:<input type="text" name="realname" value="${command.realname}" /><br />
<input type="submit" name="create" value="新增" />
</form>
</body>
</html>




update.jsp页面内容如下:
<%@ page language="java" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>update</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/update"
method="post">
用户id: <input type="text" name="id" value="${command.id}" /><br />
用户名: <input type="text" name="username" value="${command.username}" /><br />
真实姓名:<input type="text" name="realname" value="${command.realname}" /><br />
<input type="submit" value="更新" />
</form>
</body>
</html>




delete.jsp页面内容如下:
<%@ page language="java" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>delete</title>
</head>
<body>
<h1>确定要删除以下用户:</h1>
<br />
<form action="${pageContext.request.contextPath}/user/delete"
method="post">
用户id: <input type="text" name="id" value="${command.id}" /><br />
用户名: <input type="text" name="username" value="${command.username}" /><br />
真实姓名:<input type="text" name="realname" value="${command.realname}" /><br />
<input type="submit" value="确定" /><br /> <input type="submit"
value="取消" />
</form>
</body>
</html>






也可以使用委托方式:略

参考:http://jinnianshilongnian.iteye.com/blog/1752171 


你可能感兴趣的:(springMVC学习笔记三(controller控制器))