一、 Struts2的调试标签
1.1 Struts的调试标签可以查看值栈中的数据
1.在jsp中引入struts标签
<%@ taglib uri="/struts-tags" prefix="s"%>
2.在jsp中写入调试标签
<s:debug></s:debug>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>搜索
<s:debug></s:debug>
</body>
</html>
1.2 通过调试标签,查看值栈对象
点击struts调试标签,我们可以查看值栈中的数据。
值栈数据:
1.root对象(list集合)
1.action对象。
action对象的中属性值(其属性值必须存在get方法,否则struts获取不到)。
2.struts依赖对象。
3.Map集合
vs.set("11","22");
4.vs.push(new User("12","22"))
2.context对象(map集合。数据以key-value形式存在)
1.root--list
一个对root对象的引用。
2.request-Map
session-Map
application-Map
3. ac.set("11","22");
1.2.1 解析值栈对象中的list集合
1.2.2 解析值栈对象中的map集合
1.3 值栈对象中List集合
1.3.1 值栈对象中List集合中的Action对象(继承ActionSupport)
1.Action对象会被自动放入值栈对象中的List栈中。
2.值栈对象中存储的Action对象:
a.Action对象中的属性必须有get方法,才会被放入值栈对象中。
b.Action对象继承ActionSuport。
由于ActionSupport内部维护了几个变量,所以也会被放入值栈对象中。
errorMessages []
actionErrors []
actionMessages []
fieldErrors {}
locale zh_CN
errors {12=[1}
1.3.1 案例
UserAction.java
public class UserAction extends ActionSupport{
//由于Action自动入栈,所以其属性也会被压入栈中
private UserEntity u=new UserEntity();
public UserEntity getU() {
return u;
}
public String demo() {
//action信息
super.addActionMessage("12");
super.addActionMessage("13");
//action错误
super.addActionError("xxxx");
//action的字段错误
super.addFieldError("12", "12");
super.addFieldError("13", "13");
//手动入栈
ActionContext ac = ActionContext.getContext();
ValueStack vs = ac.getValueStack();
vs.push(new UserEntity());
return "index";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="xxxx" extends="struts-default">
<global-results>
<result name="input">/register.jsp</result>
</global-results>
<action name="demo" class="org.jsft.demo.UserAction" method="demo">
<result name="index">/index.jsp</result>
</action>
</package>
</struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:property value=""/>
<s:debug></s:debug>
</body>
</html>
测试:
localhost:8080/struts/demo
1.4 值栈对象中Map集合
1.值栈对象内部维护一个context对象,它是OgnlContext类型,
实质上就是个Map类型。
2.struts将所有的数据都直接或间接存储到了这个map中。
并且sturts将域对象中的数据也转化为map也存储到这个map中。
1.4.1 案例
UserAction.java
public class UserAction extends ActionSupport{
//由于Action自动入栈,所以其属性也会被压入栈中
private UserEntity u=new UserEntity();
public UserEntity getU() {
return u;
}
public String demo() {
//key:xxx,value:vvv。放入值栈对象中的map集合中
ActionContext ac = ActionContext.getContext();
ac.put("xxx", "vvv");
//key:xxx2,value:vvv2。放入值栈对象中的map集合中的request中。
Map<String,Object> request=(Map<String, Object>) ac.get("request");
request.put("xxx2", "vvv2");
return "index";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="xxxx" extends="struts-default">
<global-results>
<result name="input">/register.jsp</result>
</global-results>
<action name="demo" class="org.jsft.demo.UserAction" method="demo">
<result name="index">/index.jsp</result>
</action>
</package>
</struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:property value=""/>
<s:debug></s:debug>
</body>
</html>
测试:
localhost:8080/struts/demo
二、在jsp中,使用Struts2 取值标签取值
、
2.1 Struts2 取值标签原理
1.
值栈对象是struts数据的中转转,所有数据都存储在值栈对象中。
用户每一次请求都会创建action对象、值栈对象、以及ActionContext对象、
同时把action对象、域对象放入值栈对象中。
然后将值栈对象放入request域对象中。key:struts.valueStack。
====> ValueStack对象存储着域对象的数据、以及action对象。
2.ActionContext可以获取struts内部的其他对象。
综上:通过struts取值标签可以获取值栈对象中的所有数据。
2.2 Struts2 取值标签
使用struts2取值标签可以获取值栈对象中list集合中对象的属性。
使用struts2取值标签可以获取值栈对象中map集合中数据。
1.获取值栈对象中list集合数据。
<s:property value="u"/>
主要用户获取action对象中的成员属性。
2.获取值栈对象中map集合数据。
1、key:request/session/applicaiton (获取域对象数据)
<s:property value="#request.msg"/>
<s:property value="#session.msg"/>
<s:property value="#application.msg"/>
主要用户获取域对象中的数据。以及放入map中的数据。
2.2 key:自定义
<s:property value="#u"/>
2.3 key:attr ,自动搜索域对象
<s:property value="#attr.msg"/>
自动从request,sessuin,applicaiton中搜索。
2.4 key:parameters , 获取入参数据
<s:property value="#parametes.msg"/>
2.2.1 获取值栈对象中list的数据
<s:property value="u"/>
<s:property value="u.id"/>
<s:property value="u.name"/>
获取的是action对象中的u成员数据。
Demo.java
public class Demo extends ActionSupport{
private User u=new User(1,"小白");
public User getU() {
return u;
}
public String get() {
return "index";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="xxxx1" extends="struts-default" namespace="/" >
<action name="test" class="org.jspft.demo.Demo" method="get">
<result name="index" type="redirect">/index.jsp</result>
</action>
</package>
</struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!-- struts调试标签 -->
<s:debug></s:debug>
<!-- 取值栈对象中list中的值 -->
<s:property value="u"/>
<s:property value="u.id"/>
<s:property value="u.name"/>
</body>
</html>
2.2.2 获取值栈对象中map的数据
获取的是值栈对象的map对象中的数据,从map中取值。
<s:property value="#request.request_data"/>
获取map中key为request的对象,然后取值
<s:property value="#session.session_data"/>
获取map中key为session的对象,然后取值
<s:property value="#application.application_data"/>
获取map中key为application的对象,然后取值
<s:property value="#u"/>
获取map中key为u的对象,然后取值
Demo.java
public class Demo extends ActionSupport{
public String get() {
ActionContext ac = ActionContext.getContext();
//2.存储数据到域对象
//获取域对象对应的API
Map<String,Object> request=(Map<String, Object>) ac.get("request");
Map<String, Object> session = ac.getSession();
Map<String, Object> application = ac.getApplication();
request.put("request_data", "request_data");
session.put("session_data", "session_data");
application.put("application_data", "application_data");
return "index";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="xxxx1" extends="struts-default" namespace="/" >
<action name="test" class="org.jspft.demo.Demo" method="get">
<result name="index" type="redirect">/index.jsp</result>
</action>
</package>
</struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!-- struts调试标签 -->
<s:debug></s:debug>
<!-- 取值栈对象中map中的值 -->
<s:property value="#request.request_data"/>
<s:property value="#session.session_data"/>
<s:property value="#application.application_data"/>
</body>
</html>
2.2.3 获取值栈对象中map的数据(自动从域对象中搜索(request–》session–》aaplicaiton))
<s:property value="#attr.msg"/>
相当于el表达式中的自动搜索。即
pageContest.findAttribute("msg").
它会自动从reqest-->session-->application中取值。
Demo.java
public class Demo extends ActionSupport{
public String get() {
ActionContext ac = ActionContext.getContext();
//获取域对象对应的API
Map<String,Object> request=(Map<String, Object>) ac.get("request");
Map<String, Object> session = ac.getSession();
Map<String, Object> application = ac.getApplication();
request.put("msg", "request_data");
session.put("msg", "session_data");
application.put("msg", "application_data");
return "index";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="xxxx1" extends="struts-default" namespace="/" >
<action name="test" class="org.jspft.demo.Demo" method="get">
<result name="index" type="redirect">/index.jsp</result>
</action>
</package>
</struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!-- 取值栈对象中map中的值 -->
<s:property value="#attr.msg"/>
</body>
</html>
2.2.4 获取值栈对象中的map数据(获取请求参数)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!-- 取值栈对象中map中的值 -->
<s:property value="#parameters.name"/>
</body>
</html>
请求路径:http://localhost:8080/struts3/index.jsp?name=12&name=12
请求路径:http://localhost:8080/struts3/index.jsp?name=121212
五、在xml中,使用Struts.xml中的特殊表达式取值
Struts.xml中的特殊表达式可以获取值栈中的数据。
1.获取action中属性。
属性必须有get方法。
2.获取值栈中的数据。
5.1 在struts.xml中获取action中的数据
Demo.java
public class Demo extends ActionSupport{
private User u=new User(1,"小白");
public User getU() {
return u;
}
public String get() {
return "index";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="xxxx1" extends="struts-default" namespace="/" >
<action name="test" class="org.jspft.demo.Demo" method="get">
<result name="index" type="redirect">/index.jsp?u=${u.id}</result>
</action>
</package>
</struts>
访问测试:
5.2 在struts.xml中获取值栈中的数据
Demo.java
public class Demo extends ActionSupport{
public String get() {
ActionContext ac = ActionContext.getContext();
//获取域对象对应的API
Map<String,Object> request=(Map<String, Object>) ac.get("request");
request.put("msg", "request_data");
return "index";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="xxxx1" extends="struts-default" namespace="/" >
<action name="test" class="org.jspft.demo.Demo" method="get">
<result name="index" type="redirect">/index.jsp?u=${#request.msg}</result>
</action>
</package>
</struts>
访问测试: