一、ParameterizableViewControllerhtml
org.springframework.web.servlet.mvc.ParameterizableViewControllerjava
若是请求是/index.action的请求路径,则直接跳转到/index.jsp页面,不通过程序员定义的控制器Action。从另外一角度来理解,就是:能够隐藏真正访问.jsp文件,而使用.action进行访问。程序员
<!-- /index.action请求,直接转发到/index.jsp页面 --> <bean name="/index.action" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> <property name="viewName" value="/index.jsp"></property> </bean>
ParameterizableViewControllerweb org.springframework.web.servlet.mvc.ParameterizableViewControllerspring This controller offers an alternative to sending a request straight to a view such as a JSP. The advantage here is that the client is not exposed to the concrete view technology but rather just to the controller URL; the concrete view will be determined by the ViewResolver. mvc |
二、AbstractCommandControllerjsp
org.springframework.web.servlet.mvc.AbstractCommandControlleride
可以以实体的形式,收集客户端参数post
(1)Action类继承AbstractCommandController测试
EmpAction.java
package com.rk.web.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.validation.BindException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractCommandController; import com.rk.web.entity.Employee; public class EmpAction extends AbstractCommandController{ public EmpAction(){ this.setCommandClass(Employee.class); } @Override protected ModelAndView handle(HttpServletRequest request,HttpServletResponse response, Object command, BindException errors) throws Exception { System.out.println("EmpAction.handle()"); ModelAndView modelAndView = new ModelAndView(); Employee emp = null; if(command instanceof Employee){ emp = (Employee) command; } modelAndView.addObject("id", emp.getId()); modelAndView.addObject("name", emp.getName()); modelAndView.addObject("gender", emp.getGender()); modelAndView.addObject("hiredate", emp.getHiredate()); modelAndView.setViewName("/jsp/success.jsp"); return modelAndView; } }
(2)配置
<bean name="/emp.action" class="com.rk.web.action.EmpAction"></bean>
(3)JSP页面
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta name="content-type" content="text/html; charset=UTF-8"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <form action="${pageContext.request.contextPath}/emp.action" method="post"> <table> <tr> <td>员工ID:</td> <td><input name="id" type="text"/></td> </tr> <tr> <td>员工姓名:</td> <td><input name="name" type="text"/></td> </tr> <tr> <td>员工性别:</td> <td><input name="gender" type="text"/></td> </tr> <tr> <td>入职时间:</td> <td><input name="hiredate" type="text"/></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="添加"/> </td> </tr> </table> </form> </body> </html>
/jsp/success.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta name="content-type" content="text/html; charset=UTF-8"> <title>My SpringMVC</title> </head> <body> 当前页面:success.jsp<br> <table border="1" style="border-collapse: collapse;" align="center"> <tr> <th width="200">属性</th> <th width="200">值</th> </tr> <tr> <td>ID:</td> <td>${id}</td> </tr> <tr> <td>姓名:</td> <td> ${name}</td> </tr> <tr> <td>性别:</td> <td>${ gender}</td> </tr> <tr> <td>入职:</td> <td>${ hiredate}</td> </tr> </table> </body> </html>
(4)entity类
Emloyee.java 其中4个Field,包括三种类型:Integer、String、Date
package com.rk.web.entity; import java.util.Date; public class Employee { private Integer id; private String name; private String gender; private Date hiredate; 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 String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", gender=" + gender + ", hiredate=" + hiredate + "]"; } }
(5)测试结果:
→对于中文不支持
→对于日期不支持