首页 > 代码库 > [jbdj]SpringMVC框架(4)控制器Controller
[jbdj]SpringMVC框架(4)控制器Controller
控制器:Action继承什么类。
1}jsp->jsp直接转发
org.springframework.web.servlet.mvc.ParameterizableViewController
springmvc.xml
<!-- /index.action请求,直接转发到/index.jsp页面 --> <bean name="/index.action" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> <property name="viewName" value="/index.jsp"/> </bean>
2}以实体形式收集参数
org.springframework.web.servlet.mvc.AbstractCommandController
jsp
<form action="${pageContext.request.contextPath}/back.action" method="post"> 用户名:<input type="text" name="username" /> <p> 密码:<input type="password" name="userpewd" /> <p> 年龄:<input type="text" name="age" /> <p> 入职日期:<input type="text" name="userdate" /> <p> <input type="submit" value="提交" /> </form>
Springmvc.xml
<bean name = "/back.action" class = "BeanAction"> </bean> <!-- 映射器(框架) --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
BeanAction.java
import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.validation.BindException; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractCommandController; /** * @author Administrator * */ @SuppressWarnings("deprecation") public class BeanAction extends AbstractCommandController{ //空参构造器,注入user对象 public BeanAction() { this.setCommandClass(User.class); } /** * 自定义类型转换器,将String->Date类型(格式yyyy-MM-dd) */ @Override protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception { //向springmvc内部注入一个自定义的类型转换器 //参数一:将String转成什么类型的字节码 //参数二:自定义转换规则 //true表示该日期字段可以为空 binder.registerCustomEditor( Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true)); } @Override protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object obj, BindException error) throws Exception { //建对象 ModelAndView andView = new ModelAndView(); User user =null; if (obj instanceof User) { user = (User)obj; } System.out.println(user.toString()); andView.addObject("username", user.getUsername()); andView.addObject("userpewd", user.getUserpewd()); andView.addObject("age", user.getAge()); andView.addObject("userdate", user.getUserdate()); andView.setViewName("/back.jsp"); return andView; } }
User .java
import java.util.Date; public class User { private String username; private String userpewd; private Integer age; private Date userdate; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpewd() { return userpewd; } public void setUserpewd(String userpewd) { this.userpewd = userpewd; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getUserdate() { return userdate; } public void setUserdate(Date userdate) { this.userdate = userdate; } @Override public String toString() { return "User [username=" + username + ", userpwd=" + userpewd + ", age=" + age + ", userdate=" + userdate + "]"; } }
Back.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP ‘index.jsp‘ starting page</title> </head> <body> 这是测试网页2<br> ${username} <p> ${userpewd} <p> ${age} <p> ${userdate} <p> </body> </html>
[jbdj]SpringMVC框架(4)控制器Controller
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。