多的不说,直接上代码;

struts.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <package name="login" namespace="/user" extends="struts-default">
 <action name="*_*" class="com.wepull.struts2.action.{1}Action"
    method="{2}">
  <result name="success">{1}_{2}.jsp</result>
  <result name="input">User_toLogin.jsp</result>
 </action>
</package>
</struts>

第一种传参方式:

import com.opensymphony.xwork2.ActionSupport;
/**
 * Struts2三种传参方式(从jsp页面传到Action)
 *
 * 方式一:直接在action中写属性,然后写set、get方法
 * 1.在Action里添加属性,属性名要对应jsp上的属性名
 *  2.添加相应的set方法
 *
 */
public class UserAction extends ActionSupport {
private String name;
private String pass;
public String getName() {
 return name;
}
public void setName(String name) {
 this.name = name;
}
public String getPass() {
 return pass;
}
public void setPass(String pass) {
 this.pass = pass;
}


public String doLogin(){
 String forward="input";
 //System.out.println(name+" "+pass);
 if("wepull".equalsIgnoreCase(name)&&"123".equalsIgnoreCase(pass)){
  return SUCCESS;   //登录成功,返回成功页面
 }else{
  return forward;   //登录失败,返回重新登录
 }
}

public String toLogin(){
 return SUCCESS;       //进入登录页面
}

}

------------------------------------------------------------------------------------------------------------------------------

User_toLogin.jsp页面:

   <form action="user/User_doLogin">
    <table>
     <tr>
      <td>用户名</td>
      <td><input type="text" name="name"></td>
      <td>&nbsp;</td>
     </tr>
     <tr>
      <td>密 码</td>
      <td><input type="text" name="pass"></td>
      <td>&nbsp;</td>
     </tr>
     <tr>
      <td><input type="submit" value="sure" ></td>
      <td><input type="reset" value="reset" ></td>
      <td>&nbsp;</td>
     </tr>
    </table>
   </form>

##########################################################################

第二种传参方式:

import com.opensymphony.xwork2.ActionSupport;
/**
* Struts2三种传参方式(从jsp页面传到Action)
*
* 方式二:把属性封装到dto中,传值时以dto名字.其中属性
* 1.在Action里添加属性,属性名要对应jsp上的属性名
*  2.添加相应的set方法
*
*/
public class UserAction extends ActionSupport {
private UserDTO user;
public UserDTO getUser() {
 return user;
}
public void setUser(UserDTO user) {
 this.user = user;
}

public String doLogin(){
 String forward="input";
 //System.out.println(user.getName()+" "+user.getPass());
 if("wepull".equalsIgnoreCase(user.getName())&&"123".equalsIgnoreCase(user.getPass())){
  return SUCCESS;  
 }else{
  return forward;
 }
}

public String toLogin(){
 return SUCCESS;
}
}

------------------------------------------------------------------------------------------------------------------------------

UserDTO.java代码

public class UserDTO {
private String id;
private String name;
private String pass;
public String getId() {
 return id;
}
public void setId(String id) {
 this.id = id;
}
public String getName() {
 return name;
}
public void setName(String name) {
 this.name = name;
}
public String getPass() {
 return pass;
}
public void setPass(String pass) {
 this.pass = pass;
}
}

------------------------------------------------------------------------------------------------------------------------------

User_toLogin.jsp页面:

   <form action="user/User_doLogin">
    <table>
     <tr>
      <td>用户名</td>
      <td><input type="text" name="user.name"></td>
      <td>&nbsp;</td>
     </tr>
     <tr>
      <td>密 码</td>
      <td><input type="text" name="user.pass"></td>
      <td>&nbsp;</td>
     </tr>
     <tr>
      <td><input type="submit" value="sure" ></td>
      <td><input type="reset" value="reset" ></td>
      <td>&nbsp;</td>
     </tr>
    </table>
   </form>

##########################################################################

第三种传参方式:

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* Struts2三种传参方式(从jsp页面传到Action)
*
* 方式三:实现ModelDriven
* 实现ModelDriven接口
*
*/
public class UserAction extends ActionSupport implements ModelDriven<UserDTO>{
private UserDTO user;

public UserDTO getUser() {
 return user;
}
public void setUser(UserDTO user) {
 this.user = user;
}

public String doLogin(){
 String forward="input";
 //System.out.println(user.getName()+" "+user.getPass());
 if("wepull".equalsIgnoreCase(user.getName())&&"123".equalsIgnoreCase(user.getPass())){
  return SUCCESS;  
 }else{
  return forward;
 }
}

public String toLogin(){
 return SUCCESS;
}
public UserDTO getModel() {
 if(user==null){
  user=new UserDTO();
 }
 return user;
}
}

------------------------------------------------------------------------------------------------------------------------------

User_toLogin.jsp页面:

   <form action="user/User_doLogin">
    <table>
     <tr>
      <td>用户名</td>
      <td><input type="text" name="name"></td>
      <td>&nbsp;</td>
     </tr>
     <tr>
      <td>密 码</td>
      <td><input type="text" name="pass"></td>
      <td>&nbsp;</td>
     </tr>
     <tr>
      <td><input type="submit" value="sure" ></td>
      <td><input type="reset" value="reset" ></td>
      <td>&nbsp;</td>
     </tr>
    </table>
   </form>

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

另外,在介绍一种通过HttpServletRequest request  传参方式

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 获取request参数的方式
*HttpServletRequest request  传参方式
*
*/
public class UserAction extends ActionSupport{


public String doLogin(){
 //获取request对象
 HttpServletRequest request=ServletActionContext.getRequest();
 //获取session对象
 //HttpSession session=request.getSession();
 String name=request.getParameter("name");
 String pass=request.getParameter("pass");
 String forward="input";
 if("wepull".equalsIgnoreCase(name)&&"123".equalsIgnoreCase(pass)){
  return SUCCESS;  
 }else{
  return forward;
 }
}

public String toLogin(){
 return SUCCESS;
}

}