码迷,mamicode.com
首页 > Web开发 > 详细

初学者很实用:struts2中将jsp数据传到action的几种方式

时间:2015-07-13 18:40:16      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

先给上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;
/**
 * 方式一:直接在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> </td>
     </tr>
     <tr>
      <td>密 码</td>
      <td><input type="text" name="pass"></td>
      <td> </td>
     </tr>
     <tr>
      <td><input type="submit" value="sure" ></td>
      <td><input type="reset" value="reset" ></td>
      <td> </td>
     </tr>
    </table>
   </form>

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

第二种传参方式:

import com.opensymphony.xwork2.ActionSupport;
/**
* 方式二:把属性封装到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> </td>
     </tr>
     <tr>
      <td>密 码</td>
      <td><input type="text" name="user.pass"></td>
      <td> </td>
     </tr>
     <tr>
      <td><input type="submit" value="sure" ></td>
      <td><input type="reset" value="reset" ></td>
      <td> </td>
     </tr>
    </table>
   </form>

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

第三种传参方式:

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 方式三:实现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> </td>
     </tr>
     <tr>
      <td>密 码</td>
      <td><input type="text" name="pass"></td>
      <td> </td>
     </tr>
     <tr>
      <td><input type="submit" value="sure" ></td>
      <td><input type="reset" value="reset" ></td>
      <td> </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;
}
}


在开发中最常用的就是DTO和实现ModelDriven两种。要掌握哟!~


版权声明:本文为博主原创文章,未经博主允许不得转载。

初学者很实用:struts2中将jsp数据传到action的几种方式

标签:

原文地址:http://blog.csdn.net/a403071690/article/details/46865111

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!