码迷,mamicode.com
首页 > 编程语言 > 详细

[jbdj]SpringMVC框架(4)控制器Controller

时间:2017-07-12 10:10:17      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:mvc   eof   mini   bean   sys   handle   pos   springmvc   ide   

控制器: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

标签:mvc   eof   mini   bean   sys   handle   pos   springmvc   ide   

原文地址:http://www.cnblogs.com/yansen86/p/7153622.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!