struts2入门2--参数的传递

来源:互联网 发布:知乎费县夫妻刑警笔录 编辑:程序博客网 时间:2024/05/22 00:33

避免混乱,下面新建了第二个工程来实现 参数的传递
用三种方法来实现,第三种方法比较好
项目名 struts2Test2

重要知识点,在第三种方法里如何将参数读入集合里面,见login.jsp注释

Struts2.xml (这里是用了“指定多个配置文件” myxml.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>    <!--      -->     <include file="myxml.xml"></include> </struts> 

Myxml.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="default" namespace="/" extends="struts-default">        <action name="helloWorld" class="com.bright.action.MyAction">            <!-- result中不指定name type 属性,默认是SUCCESS   -->            <result>/index.jsp</result>        </action>        <action name="add" class="com.bright.action.MyAction">            <result>/add.jsp</result>        </action>         <!--             接收参数            1,使用Action的属性接受参数            2,使用DomainModel接收参数            3,使用ModelDriven接收参数           -->        <!--1,使用Action的属性接受参数  -->        <action name="doaction" method="login" class="com.bright.action.Doaction">            <!-- result中不指定name type 属性,默认是SUCCESS   -->            <result>/index.jsp</result>        </action>        <!--2,使用DomainModel接收参数  -->        <action name="doaction2" method="login2" class="com.bright.action.Doaction2">            <!-- result中不指定name type 属性,默认是SUCCESS   -->            <result>/index.jsp</result>        </action>        <!--3,使用ModelDriven接收参数 , 比较推荐大家使用这种方法-->        <action name="doaction3" method="login3" class="com.bright.action.Doaction3">            <!-- result中不指定name type 属性,默认是SUCCESS   -->            <result>/index.jsp</result>        </action>        </package></struts>

Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'login.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <form action="doaction3.action" method="post"> <!--  由于使用了第三种方法,而他们的name是一样的,所以有冲突,所以注释起来  第一种方法  用户名:  <input type="text" name="uname">    密码:<input type="password" name="pw">   <br><br>    第二种方法  用户名:  <input type="text" name="user.uname">    密码:<input type="password" name="user.pw">   <br><br>    -->      第三种方法  用户名:  <input type="text" name="name">    密码:<input type="password" name="pw">    选项1:  <input type="text" name="booklist[0]"> <!-- 加入list集合是一个对象集合例如是 User,那么这里 -->      选项2:  <input type="text" name="booklist[1]"> <!-- 应该是booklist[0].uname   ....以此类推 -->        选项3:  <input type="text" name="booklist[2]"> <!-- 那么获取值就是user2.getBooklist().get(0).getUname() -->    <br><br>    <input type="submit" value="提交">    </form></html>

第一种方法
1,使用Action的属性接受参数

Doaction.java

package com.bright.action;import com.opensymphony.xwork2.ActionSupport;public class Doaction extends ActionSupport {    private String uname;    private String pw;    public String getUname() {        return uname;    }    public void setUname(String uname) {        this.uname = uname;    }    public String getPw() {        return pw;    }    public void setPw(String pw) {        this.pw = pw;    }    public String login(){        System.out.println(uname);        System.out.println(pw);        return SUCCESS;    }public String add(){        return "add";    }}

2,使用DomainModel接收参数

User.java

package com.bright.action;public class User {    private String uname;    private String pw;    public String getUname() {        return uname;    }    public void setUname(String uname) {        this.uname = uname;    }    public String getPw() {        return pw;    }    public void setPw(String pw) {        this.pw = pw;    }    public User() {        super();        // TODO Auto-generated constructor stub    }    public User(String uname, String pw) {        super();        this.uname = uname;        this.pw = pw;    }}

Doaction2.java

package com.bright.action;import com.opensymphony.xwork2.ActionSupport;public class Doaction2 extends ActionSupport {    private User user;    public User getUser() {        return user;    }    public void setUser(User user) {        this.user = user;    }    public String login2(){        System.out.println(user.getUname());        System.out.println(user.getPw());        return SUCCESS;    }}

3,第三中方法,使用modelDriven,开发中最常用的咯

User2.java

package com.bright.action;import java.util.List;public class User2 {    private String name;    private String pw;    private List<String > booklist;    public List<String> getBooklist() {        return booklist;    }    public void setBooklist(List<String> booklist) {        this.booklist = booklist;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPw() {        return pw;    }    public void setPw(String pw) {        this.pw = pw;    }    public User2() {        super();        // TODO Auto-generated constructor stub    }    public User2(String name, String pw) {        super();        this.name = name;        this.pw = pw;    }}

Doaction3.java

package com.bright.action;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class Doaction3 extends ActionSupport implements ModelDriven<User2> {    User2 user2=new User2();    public String login3(){        System.out.println(user2.getName());        System.out.println(user2.getPw());        System.out.println(user2.getBooklist().get(0));        System.out.println(user2.getBooklist().get(1));        System.out.println(user2.getBooklist().get(2));        return SUCCESS;    }    public User2 getModel() {        return user2;    }}
0 0
原创粉丝点击