在struts2中,action的传值方式是如何的呢,是不是会和MVC模式与struts2一样呢,下面我们来讨论一下,它的传值方式。
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
- 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";
- System.out.println(this.username);
- this.userpassword+="789";
- 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);
- System.out.println("***"+this.userpassword);
- 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>
这样,就可以实现action之间的传递了,通常建议使用传值方法二。