Struts2的三种传值方式

摘抄自别人的。。。

原文地址:http://blog.csdn.net/manchester117/article/details/51543566

1.普通的传值方式

UserActionForCommonParam类

Action类接收三个参数,分别是id,username,content.

package com.struts.action;

public class UserActionForCommonParam {
private int id;
private String username;
private String content;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

// 从前台页面接收到参数后会在这个方法里打印
public String addUser() {
System.out.println("ID: " + this.getId());
System.out.println("Username: " + this.getUsername());
System.out.println("Content: " + this.getContent());
return "success";
}
}

对应的struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.devMode" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<action name="UserActionForCommonParam" class="com.struts.action.UserActionForCommonParam" method="addUser">
<result name="success">/UserPage.jsp</result>
</action>
</package>
</struts>

前台页面UserPage.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>UserPage</title>
</head>
<body>
<%--下面的Table用于提交参数(id,username,content)--%>
<form action="UserActionForCommonParam" method="POST">
<table border="1">
<tr>
<td>ID:</td>
<td><input type="text" name="id"/></td>
</tr>
<tr>
<td>Username:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>Content:</td>
<td><input type="text" name="content"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
<br/>
<%--下面的Table用于显示传递回来id,username,content--%>
<table border="1">
<tr>
<td>ID: </td>
<td><input type="text" value="${id}"/></td>
</tr>
<tr>
<td>Username: </td>
<td><input type="text" value="${username}"/></td>
</tr>
<tr>
<td>Content: </td>
<td><input type="text" value="${content}"/></td>
</tr>
</table>
<%--用于查看ValueStack中的传递的值--%>
<s:debug/>
</body>
</html>

在实践后会发现,如果遇到参数非常多的情况,那么就需要在Action类中写非常多的属性以及对应的get/set方法.所以这种方式不太可取.解决问题的方法必然是封装一个JavaBean.这就用到了Strut2的第二种传值方式--DomainModel

 

2.DomainModel传值

首先要创建一个存储的JavaBean

User类

把id,username,content封装的到一个User类当中.

package com.struts.model;

public class User {
private int id;
private String username;
private String content;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}

UserActionForDomainModel类

package com.struts.action;

import com.opensymphony.xwork2.ActionSupport;
import com.struts.model.User;

public class UserActionForDomainModel extends ActionSupport{
private User user;

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public String addUser() {
System.out.println("ID: " + user.getId());
System.out.println("Username: " + user.getUsername());
System.out.println("Content: " + user.getContent());
return "success";
}
}

对应的struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.devMode" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<action name="userForDomainModel" class="com.struts.action.UserActionForDomainModel" method="addUser">
<result name="success">/UserPage.jsp</result>
</action>
</package>
</struts>

前台页面UserPage.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>UserPage</title>
</head>
<body>
<%--要特别注意的是DomainModel的传值方式必须要使用对象.参数方式进行赋值以及取值--%>
<form action="userForDomainModel" method="POST">
<table border="1">
<tr>
<td>ID:</td>
<td><input type="text" name="user.id"/></td>
</tr>
<tr>
<td>Username:</td>
<td><input type="text" name="user.username"/></td>
</tr>
<tr>
<td>Content:</td>
<td><input type="text" name="user.content"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
<br/>
<%--下面的Table用于显示传递回来id,username,content--%>
<table border="1">
<tr>
<td>ID: </td>
<td><input type="text" value="${user.id}"/></td>
</tr>
<tr>
<td>Username: </td>
<td><input type="text" value="${user.username}"/></td>
</tr>
<tr>
<td>Content: </td>
<td><input type="text" value="${user.content}"/></td>
</tr>
</table>
<%--用于查看ValueStack中的传递的值--%>
<s:debug/>
</body>
</html>

实际上User类不需要显式的实例化,struts会自动帮你实例化,但前提条件是,传值时需要使用对象.参数名的方式进行传递.

 

除了这种传值方式外,struts2还提供另外一种传值方式.

 

3.ModelDriven传值

依然要创建User的JavaBean

User类

...

UserActionForModelDriven类

package com.struts.action;

import com.opensymphony.xwork2.ModelDriven;
import com.struts.model.User;

public class UserActionForModelDriven implements ModelDriven<User>{
private User user;

public String addUser() {
System.out.println("ID: " + user.getId());
System.out.println("Username: " + user.getUsername());
System.out.println("Content: " + user.getContent());
return "success";
}

@Override
public User getModel() {
if (user == null) {
user = new User();
}
return user;
}
}

这种方式可以不用在Action类中编写对应的get/set方法,但是需要实例化User类.

 

前台UserPage.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>UserPage</title>
</head>
<body>
<form action="userForModelDriven" method="POST">
<table border="1">
<tr>
<td>ID:</td>
<td><input type="text" name="id"/></td>
</tr>
<tr>
<td>Username:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>Content:</td>
<td><input type="text" name="content"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
<br/>
<%--下面的Table用于显示传递回来id,username,content--%>
<table border="1">
<tr><span id="transmark"></span>
<td>ID: </td>
<td><input type="text" value="${id}"/></td>
</tr>
<tr>
<td>Username: </td>
<td><input type="text" value="${username}"/></td>
</tr>
<tr>
<td>Content: </td>
<td><input type="text" value="${content}"/></td>
</tr>
</table>
<%--用于查看ValueStack中的传递的值--%>
<s:debug/>
</body>
</html>

页面还是和普通传值一样.

posted @ 2018-03-09 11:46  无月行  阅读(185)  评论(0编辑  收藏  举报