struts2中action的传值方式

1、首先写个jsp页面

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

2、然后写LoginAction与LogoutAction//loginAction向LogoutAction传值

package myclass.struts2.action;   
  
public class LoginAction {   
    private String username;   
    private String userpassword;   
public String getUsername() {   
        return username;   
    }   
  
    public void setUsername(String username) {   
        this.username = username;   
    }   
  
    public String getUserpassword() {   
        return userpassword;   
    }   
  
    public void setUserpassword(String userpassword) {   
        this.userpassword = userpassword;   
    }   
  
public String execute(){   
    System.out.println("我进来了呢");   
    System.out.println(this.getUsername());   
    System.out.println(this.getUserpassword());   
    if("liping".equals(this.getUsername())&"123456".equals(this.getUserpassword())){   
        this.username+="abc";   //重点在这,使用的是this
        System.out.println(this.username);   
        this.userpassword+="789";   //重点在这,使用的是this
        System.out.println(this.userpassword);   
        return "succ";   
    }else{   
        return "fail";   
    }   
}   
}
package myclass.struts2.action;   
  
public class LogoutAction {   
    private String username;   
    private String userpassword;   
public String getUserpassword() {   
        return userpassword;   
    }   
    public void setUserpassword(String userpassword) {   
        this.userpassword = userpassword;   
    }   
public String getUsername() {   
        return username;   
    }   
    public void setUsername(String username) {   
        this.username = username;   
    }   
public String execute(){   
    System.out.println("***"+this.username);   //这里取值用的也是this
    System.out.println("***"+this.userpassword);   //这里取值用的也是this
    System.out.println("退出系统");   
    return "exit";   
       
}   
}

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

<?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE struts PUBLIC    
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
    "http://struts.apache.org/dtds/struts-2.0.dtd">  
    <struts>  
    <constant name="struts.action.extension" value="lp" />  
    <package name="login" namespace="/" extends="struts-default">  
    <action name="login" class="myclass.struts2.action.LoginAction">     
     <!--使用重定向的跳转方式 -->  
    <result name="succ" type="redirectAction">  
    <!-- 不同包action的访问   
    <param name="actionName">logout</param>  
     -->  
    <!--不同包action的访问并传值方法一    
    <param name="actionName">logout?username=${username}&userpassword=${userpassword}</param>  
     -->  
     <!--不同包action的访问并传值方法二 -->  
     <param name="actionName">logout</param>  
     <param name="username">${username}</param>  
     <param name="userpassword">${userpassword}</param>  
    <param name="namespace">/logout</param>  
    </result>  
    <result name="fail">/failure.jsp</result>  
    </action>  
    </package>  
    <package name="logout" namespace="/logout" extends="struts-default">  
    <action name="logout" class="myclass.struts2.action.LogoutAction">    
    <result name="exit">/exit.jsp</result>  
    </action>  
    </package>  
    </struts>


转载请标明原文网址:http://blog.csdn.net/lipingqingqing/article/details/6502455


转载于:https://my.oschina.net/fuluS/blog/354478

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值