MultiActionController 练习

来源:互联网 发布:java只能做网站吗 编辑:程序博客网 时间:2024/05/14 23:39

User.java

public class User {
  private String name;
  private String password;
  private Integer age;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getPassword() {
  return password;
 }

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

 public Integer getAge() {
  return age;
 }

 public void setAge(Integer age) {
  this.age = age;
 }
}

Product.java

import java.util.Date;

public class Product {
 private String name;
 private double price;
 private Date date;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public double getPrice() {
  return price;
 }

 public void setPrice(double price) {
  this.price = price;
 }

 public Date getDate() {
  return date;
 }

 public void setDate(Date date) {
  this.date = date;
 }
}

package Multiaction_AbstractCommand;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;

public class MultiTest extends MultiActionController {

 @Override
 protected void initBinder(HttpServletRequest request,
   ServletRequestDataBinder binder) throws Exception {
  super.initBinder(request, binder);
  SimpleDateFormat format = new SimpleDateFormat("yyyy-yy-dd");
  binder.registerCustomEditor(Date.class, new CustomDateEditor(format,
    true));
  binder.registerCustomEditor(Integer.class, new CustomNumberEditor(
    Integer.class, true));
  binder.registerCustomEditor(Double.class, new CustomNumberEditor(
    Double.class, true));
 }

 public ModelAndView getUser(HttpServletRequest request,
   HttpServletResponse resonse, User user) throws Exception {
  ServletRequestDataBinder binder = createBinder(request, user);
  binder.bind(request);
  System.out.println(user.getName() + user.getPassword());
  ModelAndView mav = new ModelAndView("admin/showuser");
  mav.addObject("user", user);
  return mav;
 }

 public ModelAndView getProduct(HttpServletRequest request,
   HttpServletResponse resonse, Product product) throws Exception {
  Product pro = product;
  /**
   * ServletRequestDataBinder binder = createBinder(request, product);
   * binder.bind(request); 如果这里把这里的两行代码注释去掉了,哪么bind()方法里面的绑定就没有用了
   * 所以,若果想让bind()方法的绑定没有就直接在这里再用请求的参数重新绑定了
   */
  ModelAndView mav = new ModelAndView("admin/showproduct");
  mav.addObject("product", pro);
  return mav;
 }

 public ModelAndView user(HttpServletRequest request,
   HttpServletResponse resonse) {
  ModelAndView mav = new ModelAndView("admin/user");
  return mav;
 }

 public ModelAndView product(HttpServletRequest request,
   HttpServletResponse resonse) {
  ModelAndView mav = new ModelAndView("admin/product");
  return mav;
 }

 @Override
 /**
  *
  * 进入controller首次调用,不管是否有form表单的提交 在这里可以根据参数进行页面跳转或者其他操作
  */
 protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
   HttpServletResponse arg1) throws Exception {
  System.out.println("我是handleRequestInternal方法");
  System.out.println("第一次调用");
  return super.handleRequestInternal(arg0, arg1);
 }

 @Override
 /**
  * 当有form表单提交的时候第二次调用 这个方法一般没有必要来重写,除非很特殊的情况,要改变命令对象
  */
 protected Object newCommandObject(Class clazz) throws Exception {
  System.out.println("我是newCommandObject方法:" + clazz.getSimpleName());
  System.out.println("第二次调用");
  return super.newCommandObject(clazz);
 }

 @Override
 /**
  * 当有form表单提交的时候第三次调用 当bind()方法调用了以后又重新调用此方法,即第五次调用 这个方法仅仅只是获得命令对象的类名
  */
 protected String getCommandName(Object command) {
  System.out.println("我是getCommandName()方法。我的名字是:"
    + command.getClass().getSimpleName());
  System.out.println("第三次调用或者第五次调用");
  return super.getCommandName(command);
 }

 @Override
 /**
  * 当有form表单提交的时候第四次调用 重写此方法可以绑定一些特殊的属性到命令对象
  */
 protected void bind(HttpServletRequest arg0, Object arg1) throws Exception {
  super.bind(arg0, arg1);
  System.out.println("我是bind方法");
  if (arg1.getClass().equals(User.class)) {
   System.out.println("我是User");
  }
  if (arg1.getClass().equals(Product.class)) {
   System.out.println("我是Product");
   Product product = (Product) arg1;   
   product.setName("阿迪达斯");
   product.setPrice(128.0);
   product.setDate(new Date());
  }
  System.out.println("第四次调用");
 }

}

 

product.jsp

<%@ page contentType="text/html;charset=gb2312"%>
<%@ include file="/WEB-INF/jsp/common/taglib.jsp"%>
<html>
 <head>
  <title>product</title>
 </head>
 <body>
  <form action="multi.do?method=getProduct" method="post">
   <table>
    <tr>
     <td>
      产品名称
     </td>
     <td>
      <input type="text" name="name" />
     </td>
    </tr>
    <tr>
     <td>
      产品价格
     </td>
     <td>
      <input type="text" name="price" />
     </td>
    </tr>
    <tr>
     <td>
      生产日期
     </td>
     <td>
      <input type="text" name="date" />
     </td>
    </tr>
    <tr>
     <td colspan="2">
      <input type="submit" value="提交" />
      &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="reset" value="取消" />
     </td>
    </tr>
   </table>
  </form>
 </body>
</html>

user.jsp

 

<%@ page contentType="text/html;charset=gb2312"%>
<%@ include file="/WEB-INF/jsp/common/taglib.jsp"%>
<html>
 <head>
  <title>user<title>
</head>
 <body>
  <form action="multi.do?method=getUser" method="post">
   <table>
    <tr>
     <td>
      用户名:
     </td>
     <td>
      <input type="text" name="name" />
     </td>
    </tr>
    <tr>
     <td>
      密码
     </td>
     <td>
      <input type="password" name="password" />
     </td>
    </tr>
    <tr>
     <td>
      年龄
     </td>
     <td>
      <input type="text" name="age" />
     </td>
    </tr>
    <tr>
     <td colspan="2">
      <input type="submit" value="提交" />
      <input type="reset" value="取消" />
     </td>
    </tr>
   </table>
  </form>
 </body>
</html>

showuser.jsp

<%@ page contentType="text/html;charset=gb2312"%>
<%@ include file="/WEB-INF/jsp/common/taglib.jsp"%>
<html>
 <head>
  <title>登录页面</title>
  <validator:javascript dynamicJavascript="true" formName="user"
   staticJavascript="false" xhtml="true" cdata="false" />
 </head>
 <body>
  用户名:${user.name}
  <br>
  用户密码:${user.password}
  <br>
  年龄:${user.age}
 </body>
</html>

 

showproduct.jsp

<%@ page contentType="text/html;charset=gb2312"%>
<%@ include file="/WEB-INF/jsp/common/taglib.jsp"%>
<html>
 <head>
  <title>登录成功页面</title>
 </head>
 <body>
  
  产品名称:${product.name}
  <br>
  产品价格:${product.price}
  <br>
  生产日期:${product.date}
  
 </body>
</html>

xml 配置

<bean id="methodNameResolver"
  class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
  <property name="defaultMethodName" value="index" />
  <property name="paramName" value="method" />
 </bean>
 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>

 <bean id="handlerMapping"
  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
   <value>multi.do=multi</value>
  </property>
 </bean>
 <bean id="multi" class="Multiaction_AbstractCommand.MultiTest">
  <property name="methodNameResolver" ref="methodNameResolver" />
 </bean>

 

原创粉丝点击