码迷,mamicode.com
首页 > 其他好文 > 详细

struts2传值的四种方式

时间:2020-05-22 15:36:24      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:html   getc   自动   char   ret   res   constant   编写   前端   

Struts2的4种传值方式:
1.通过request/session等传值
可通过ServletActionContext.getRequest()/getSession()等方法来获得request/session对象,

然后调用其中的setAttribute(),getAttribute(),getParameter()方法来传值。

public class UserAction{


public String addUser() {

//通过request
int id=ServletActionContext.getRequest().getParameter("id");
String username= ServletActionContext.getRequest().getParameter("username");

//通过session
ServletActionContext.getRequest().getSession().getAttribute("username");

//通过session
ActionContext.getContext().getSession().getAttribute("username");

System.out.println(username+" "+id);

return "success";

}

}
2.普通传值,一个javabean,xml配置文件
public class UserAction{
private int id;
private String username;

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 addUser() {
System.out.println("ID: " + this.getId());
System.out.println("Username: " + this.getUsername());
return "success";
}
}
<?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="UserAction" class="com.struts.action.UserAction" method="addUser">
<result name="success">/UserPage.jsp</result>
</action>
</package>
</struts>
前端页面:

<%@ 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="UserAction" 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>

<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>

</table>
<%--用于查看ValueStack中的传递的值--%>
<s:debug/>
</body>
</html>

3.使用model传值:建立一个model类Uesr来存储数据,一个UserAction要继承ActionSupport来操作,
public class UserAction extends ActionSupport
xml配置,User类不需要显式的实例化,struts会自动帮你实例化,但前提条件是,传值时需要使用对象.参数名的方式进行传递.比如form表单的输入要写成name=“user.name”
<input type="text" name="user.username"/>
public class User{
private int id;
private String username;

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 class UserAction 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());
return "success";
}
}
<?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="userAction" class="com.struts.action.UserAction" method="addUser">
<result name="success">/UserPage.jsp</result>
</action>
</package>
</struts>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>UserPage</title>
</head>
<body>
<%--要特别注意的是传值方式必须要使用对象.参数方式进行赋值以及取值--%>
<form action="userAction" 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 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>

</table>
<%--用于查看ValueStack中的传递的值--%>
<s:debug/>
</body>
</html>
4.同样是使用model来传值,还是创建一个model类User,UserAction操作类要实现接口:
public class UserAction implements ModelDriven<User>
不同的是在UserAction中可以不用编写对应的get/set方法,但是需要实例化User类.但前端页面form表单的name可以直接写对应User类的属性名
<input type="text" name="username"/>
public class User{
private int id;
private String username;

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 class UserAction implements ModelDriven<User>{
private User user;

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

@Override
public User getModel() {
if (user == null) {
user = new User();
}
return user;
}
}
<%@ 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 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>

</table>
<%--用于查看ValueStack中的传递的值--%>
<s:debug/>
</body>
</html>



原文链接:https://blog.csdn.net/weixin_39220472/article/details/80296890

struts2传值的四种方式

标签:html   getc   自动   char   ret   res   constant   编写   前端   

原文地址:https://www.cnblogs.com/yuanlinjie/p/12937406.html

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