一、用el表达式获取值栈的内容
1、代码
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>
</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" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>
<!-- 使用foreach+el表达式获取值 -->
<c:forEach items="${list }" var="user">
${user.username }<br>
${user.password }<br>
${user.type }<br>
</c:forEach>
</body>
</html>
2、运行结果
image.png二、el表达式为什么能获取表单数据
1、el表达式获取域对象
2、向域对象放值使用setAttribute,获取使用getAttribute方法
3、底层增强request对象里面getAttribute方法
- 首先从request域获取值,如果获取到,直接返回
- 如果获取不到request域的值,到值栈中把值获取出来,把值放到域对象里
4、查看源代码
image.png image.png
网友评论