• 2.vo传参模式和ModerDriven传参模式


    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html

    Copy上面的myStruts2项目,改名为myStruts2Vo项目。作如下修改:在LoginAction中有两个字段:username,password。把此两个属性重构到com.asm.vo.User类中,然后在LoginAction中提供User对象及相应的get/set方法。现在需要注意的是在login.jsp中会有如下的修改:

    户名:<input type="text" name="user.username"><br>

    密码:<input type="password" name="user.password"><br>

    关键就是改掉name属性值。其它基本无变动。 后话:假如此此User对象并不能和Model层的相应对象完全对应,我们还应借助此User对象在Action中构建出Model层的相应对象,这样,在exectue方法中便能通过构建的Model对象作为参数与Model层交互。

     1 package com.asm;
     2 
     3 import com.asm.vo.User;
     4 import com.opensymphony.xwork2.Action;
     5 
     6 public class LoginAction implements Action{
     7 
     8     private User user = new User();
     9     
    10     public User getUser() {
    11         return user;
    12     }
    13 
    14     public void setUser(User user) {
    15         this.user = user;
    16     }
    17 
    18     @Override
    19     public String execute() throws Exception {
    20         if("struts2".equals(user.getUsername())){
    21             return "loginSuccess";
    22         }else{
    23             return "loginFailure";
    24         }
    25     }
    26 
    27     
    28 }
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <form action="<%=request.getContextPath()%>/login.action" method="get">
    11         用户名:<input type="text" name="user.username"><br/>
    12         密码:<input type="password" name="user.password"><br/>
    13         <input type="submit" value="login">
    14     </form>
    15 </body>
    16 </html>

    Copy上面的myStruts2Vo项目,改名为myStruts2Model项目。重点是修改LoginAction,修改后的主要内容如下:

     1 package com.asm;
     2 
     3 import com.asm.vo.User;
     4 import com.opensymphony.xwork2.Action;
     5 import com.opensymphony.xwork2.ModelDriven;
     6 
     7 public class LoginAction implements Action,ModelDriven<User>{
     8 
     9     private User user = new User();
    10 
    11     @Override
    12     public String execute() throws Exception {
    13         if("struts2".equals(user.getUsername())){
    14             return "loginSuccess";
    15         }else{
    16             return "loginFailure";
    17         }
    18     }
    19 
    20     @Override
    21     public User getModel() {
    22         return user;
    23     }
    24 
    25     
    26 }
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <form action="<%=request.getContextPath()%>/login.action" method="get">
    11         用户名:<input type="text" name="username"><br/>
    12         密码:<input type="password" name="password"><br/>
    13         <input type="submit" value="login">
    14     </form>
    15 </body>
    16 </html>

    说明:它实现了ModelDriven接口,并使用了泛性机制(必须),因此要求jdk1.5以上。
    现在需要注意的是在login.jsp中name属性值为User中两个字段,和第一个实例一样。说明:此方式一般不会使用,在此略作了解。

  • 相关阅读:
    RabbitMQ学习之:(一)初识、概念及心得
    【转】2015年最适合去的7类互联网创业公司
    java性能
    人生中一定要坚守的格言
    AJAX学习
    三层架构
    Linux下配置文件的位置
    进程池
    Python性能测试
    彼得原则、墨菲原则、帕金森定律分别是指的什么?
  • 原文地址:https://www.cnblogs.com/sharpest/p/5579969.html
Copyright © 2020-2023  润新知