struts2 从值栈中获取数据
2018-05-18 本文已影响0人
DouDouZH
使用struts标签+ognl获取值栈的数据
<s:propery=ty value="ognl表达式"/>
一、获取字符串
1、步骤
- 向值栈中放入字符串(第三种方法)
- action配置到一个页面
- 在页面中取值
2、代码
ValuesStackAction2.java
package work.zhangdoudou.Action;
import com.opensymphony.xwork2.ActionSupport;
public class ValuesStackAction2 extends ActionSupport{
//1定义
private String name;
//2生成个get方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String execute() throws Exception {
//在执行的方法里向变量设置值
name="wangwu";
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="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="userAction2" class="work.zhangdoudou.Action.ValuesStackAction2" method="execute">
<result name="success">/ValuesStack.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>struts2_ognl</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
配置界面ValuesStack.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'ValuesStackAction.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>
<body>
<s:debug></s:debug>
<!-- 获取字符串值 -->
<s:property value="name"/>
</body>
</html>
3、运行效果
image.png二、获取对象
1、步骤
- 在值栈里放一个对象
- 给对象设置值
- action配置到一个页面
- 在页面中获取值栈中对象的值
2、代码
User.java
package work.zhangdoudou.Bean;
public class User {
private String username;
private String password;
private String type;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
ValuesStackAction3.java
package work.zhangdoudou.Action;
import com.opensymphony.xwork2.ActionSupport;
import work.zhangdoudou.Bean.User;
public class ValuesStackAction3 extends ActionSupport{
//1定义对象变量
private User user;
//2生成个get方法
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
//向值栈的user放对象
user= new User();
user.setUsername("zhangsan");
user.setPassword("123");
user.setType("student");
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="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="userAction3" class="work.zhangdoudou.Action.ValuesStackAction3" method="execute">
<result name="success">/ValuesStack.jsp</result>
</action>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>struts2_ognl</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
配置界面ValuesStack.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'ValuesStackAction.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>
<body>
<s:debug></s:debug>
获取对象的值:<br/>
<s:property value="user.username"/><br>
<s:property value="user.password"/><br>
<s:property value="user.type"/><br>
</body>
</html>
3、运行结果
image.png三、获取list
1、步骤
- 在值栈里放一个list
- 给list设置值
- action配置到一个页面
- 在页面中获取值栈中list的值
2、代码
User.java
package work.zhangdoudou.Bean;
public class User {
private String username;
private String password;
private String type;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
ValuesStackAction4.java
package work.zhangdoudou.Action;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import work.zhangdoudou.Bean.User;
public class ValuesStackAction4 extends ActionSupport{
//1定义list变量
private List<User> list;
//2生成个get方法
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
@Override
public String execute() throws Exception {
//向list存放数据
User user1=new User();
list=new ArrayList<User>();
user1.setUsername("zhangsan");
user1.setPassword("123");
user1.setType("student");
User user2=new User();
user2.setUsername("lisi");
user2.setPassword("456");
user2.setType("student1");
list.add(user1);
list.add(user2);
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="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="userAction4" class="work.zhangdoudou.Action.ValuesStackAction4" method="execute">
<result name="success">/ValuesStack.jsp</result>
</action>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>struts2_ognl</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
配置界面ValuesStack.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'ValuesStackAction.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>
<body>
<s:debug></s:debug>
获取 list值第一种死遍历:<br/>
<s:property value="list[0].username"/><br>
<s:property value="list[0].password"/><br>
<s:property value="list[0].type"/><br>
<s:property value="list[1].username"/><br>
<s:property value="list[1].password"/><br>
<s:property value="list[1].type"/><br>
<hr/>
获取 list值第二种iterator标签:<br/>
<!-- 用标签iterator标签 -->
<s:iterator value="list">
<!-- 遍历list得到list里头的每个user对象 -->
<s:property value="username"/><br>
<s:property value="password"/><br>
<s:property value=" type"/><br>
</s:iterator>
<hr/>
获取 list值第三种iterator标签:<br/>
<!-- 用标签iterator标签
用两个属性value 和var
-->
<s:iterator value="list" var="user">
<!-- 遍历list得到list里头的每个user对象
机制:把每次遍历的user对象放到context里面
获取context里面的数据:写ognl表达式
-->
<s:property value="#user.username"/><br>
<s:property value="#user.password"/><br>
<s:property value="#user.type"/><br>
</s:iterator>
</body>
</html>
3、运行效果
image.png<s:iterator value="list" var="user">加了var属性会把值临时放进context中
image.png