struts2中action的传值方式

在struts2中,action的传值方式是如何的呢,是不是会和MVC模式与struts2一样呢,下面我们来讨论一下,它的传值方式。

1、首先写个jsp页面

Code:
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <html>  
  3.   <head>  
  4.   <title> 登录页面 </title>  
  5.   <link rel=stylesheet href="css/login.css" type="text/css">  
  6.   </head>  
  7.   <body>  
  8.   <h1 align="center">用户登录页面</h1>  
  9.   <hr>  
  10.    <div align="center">  
  11.    <form action="login.lp" method="post">  
  12.    <table  cellspacing=5 border=5 bodercolor=#ffaa00 >  
  13.    <tr><th colspan="3" align="center"  bgcolor=#ffaa00>用户登录</th></tr>  
  14.    <tr>  
  15.    <th rowspan="3" background="images/2.jpg" style="width=90px"></th>  
  16.    <td>用户名:</td><td><input type="text" class="message" name="username"></td></tr>  
  17.    <tr><td>密  码:</td><td><input class="message" type="password" name="userpassword"></td></tr>  
  18.     <tr><td colspan="2" align="center"><input type="submit" value="登录">  <input type="reset" value="重置"></td></tr>  
  19.    </table>  
  20.    </form>  
  21.    </div>  
  22.   </body>  
  23. </html>  

2、然后写LoginAction与LogoutAction

Code:
  1. package myclass.struts2.action;   
  2.   
  3. public class LoginAction {   
  4.     private String username;   
  5.     private String userpassword;   
  6. public String getUsername() {   
  7.         return username;   
  8.     }   
  9.   
  10.     public void setUsername(String username) {   
  11.         this.username = username;   
  12.     }   
  13.   
  14.     public String getUserpassword() {   
  15.         return userpassword;   
  16.     }   
  17.   
  18.     public void setUserpassword(String userpassword) {   
  19.         this.userpassword = userpassword;   
  20.     }   
  21.   
  22. public String execute(){   
  23.     System.out.println("我进来了呢");   
  24.     System.out.println(this.getUsername());   
  25.     System.out.println(this.getUserpassword());   
  26.     if("liping".equals(this.getUsername())&"123456".equals(this.getUserpassword())){   
  27.         this.username+="abc";   
  28.         System.out.println(this.username);   
  29.         this.userpassword+="789";   
  30.         System.out.println(this.userpassword);   
  31.         return "succ";   
  32.     }else{   
  33.         return "fail";   
  34.     }   
  35. }   
  36. }   
  37.   
  38.   
  39. package myclass.struts2.action;   
  40.   
  41. public class LogoutAction {   
  42.     private String username;   
  43.     private String userpassword;   
  44. public String getUserpassword() {   
  45.         return userpassword;   
  46.     }   
  47.     public void setUserpassword(String userpassword) {   
  48.         this.userpassword = userpassword;   
  49.     }   
  50. public String getUsername() {   
  51.         return username;   
  52.     }   
  53.     public void setUsername(String username) {   
  54.         this.username = username;   
  55.     }   
  56. public String execute(){   
  57.     System.out.println("***"+this.username);   
  58.     System.out.println("***"+this.userpassword);   
  59.     System.out.println("退出系统");   
  60.     return "exit";   
  61.        
  62. }   
  63. }   

3、接下来就需要在struts.xml中来配置他们了。

Code:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.  <!DOCTYPE struts PUBLIC    
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.     <struts>  
  6.     <constant name="struts.action.extension" value="lp" />  
  7.     <package name="login" namespace="/" extends="struts-default">  
  8.     <action name="login" class="myclass.struts2.action.LoginAction">     
  9.      <!--使用重定向的跳转方式 -->  
  10.     <result name="succ" type="redirectAction">  
  11.     <!-- 不同包action的访问   
  12.     <param name="actionName">logout</param>  
  13.      -->  
  14.     <!--不同包action的访问并传值方法一    
  15.     <param name="actionName">logout?username=${username}&userpassword=${userpassword}</param>  
  16.      -->  
  17.      <!--不同包action的访问并传值方法二 -->  
  18.      <param name="actionName">logout</param>  
  19.      <param name="username">${username}</param>  
  20.      <param name="userpassword">${userpassword}</param>  
  21.     <param name="namespace">/logout</param>  
  22.     </result>  
  23.     <result name="fail">/failure.jsp</result>  
  24.     </action>  
  25.     </package>  
  26.     <package name="logout" namespace="/logout" extends="struts-default">  
  27.     <action name="logout" class="myclass.struts2.action.LogoutAction">    
  28.     <result name="exit">/exit.jsp</result>  
  29.     </action>  
  30.     </package>  
  31.     </struts>  

这样,就可以实现action之间的传递了,通常建议使用传值方法二。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值