property标签用于取得value的属性值。
1.显示Action中的属性值:<s:property value="属性名" />
2.显示字符串,使用单引号:<s:property value="'字符串'" />
3.显示默认值,Action中找不到指定的属性时,显示default属性的值:<s:property value="属性名" default="默认值" />
4.解析HTML字符串,escape默认值为true,直接输出字符串,escape设为false时,解析HTML字符串:<s:property value="'<font color=\"red\">Red</font>'" escape="false" />
实例:
web.xml:
01.
<?xml version="1.0" encoding="UTF-8"?>
02.
<web-app version="2.5"
03.
xmlns="http://java.sun.com/xml/ns/javaee"
04.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05.
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ;
07.
<welcome-file-list>
08.
<welcome-file>hello.jsp</welcome-file>
09.
</welcome-file-list>
10.
<filter>
11.
<filter-name>struts2</filter-name>
12.
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
13.
</filter>
14.
<filter-mapping>
15.
<filter-name>struts2</filter-name>
16.
<url-pattern>/*</url-pattern>
17.
</filter-mapping>
18.
</web-app>
struts.xml:
01.
<?xml version="1.0" encoding="UTF-8" ?>
02.
<!DOCTYPE struts PUBLIC
03.
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
05.
06.
<struts>
07.
<!--
08.
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
09.
<constant name="struts.devMode" value="false" />
10.
11.
<include file="example.xml"/>
12.
13.
14.
15.
<package name="default" namespace="/" extends="struts-default">
16.
<default-action-ref name="index" />
17.
<action name="index">
18.
<result type="redirectAction">
19.
<param name="actionName">HelloWorld</param>
20.
<param name="namespace">/example</param>
21.
</result>
22.
</action>
23.
</package>
24.
-->
25.
26.
<!-- Add packages here -->
27.
<constant name="struts.devMode" value="true" />
28.
<constant name="struts.i18n.encoding" value="GBK" />
29.
<package name="user" namespace="/" extends="struts-default">
30.
<action name="user" class="cn.edu.ahau.mgc.struts2.action.UserAction">
31.
<result>/addSuccess.jsp</result>
32.
</action>
33.
</package>
34.
</struts>
UserAction.java:
01.
package cn.edu.ahau.mgc.struts2.action;
02.
03.
import com.opensymphony.xwork2.ActionSupport;
04.
05.
public class UserAction extends ActionSupport {
06.
07.
private String userName;
08.
09.
10.
public String execute() {
11.
return SUCCESS;
12.
}
13.
14.
public String getUserName() {
15.
return this.userName;
16.
}
17.
18.
public void setUserName(String userName) {
19.
this.userName = userName;
20.
}
21.
22.
}
index.jsp:
01.
<%@ page language="java" pageEncoding="GB18030"%>
02.
<%
03.
String path = request.getContextPath();
04.
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
05.
%>
06.
07.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
08.
<html>
09.
<head>
10.
<base href="<%=basePath%>">
11.
12.
<title>Property</title>
13.
<meta http-equiv="pragma" content="no-cache">
14.
<meta http-equiv="cache-control" content="no-cache">
15.
<meta http-equiv="expires" content="0">
16.
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17.
<meta http-equiv="description" content="This is my page">
18.
<!--
19.
<link rel="stylesheet" type="text/css" href="styles.css">
20.
-->
21.
</head>
22.
23.
<body>
24.
<a href="user?userName=magci">user?userName=magci</a><br />
25.
</body>
26.
</html>
addSuccess.jsp:
01.
<%@ page language="java" pageEncoding="GB18030"%>
02.
<%@ taglib uri="/struts-tags" prefix="s" %>
03.
<%
04.
String path = request.getContextPath();
05.
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
06.
%>
07.
08.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
09.
<html>
10.
<head>
11.
<base href="<%=basePath%>">
12.
13.
<title>Property</title>
14.
<meta http-equiv="pragma" content="no-cache">
15.
<meta http-equiv="cache-control" content="no-cache">
16.
<meta http-equiv="expires" content="0">
17.
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18.
<meta http-equiv="description" content="This is my page">
19.
<!--
20.
<link rel="stylesheet" type="text/css" href="styles.css">
21.
-->
22.
</head>
23.
24.
<body>
25.
User Add Success! <br />
26.
显示Action中的属性值: <s:property value="userName" /><br />
27.
显示字符串: <s:property value="'userName'" /><br />
28.
Action中找不到指定的属性时,显示默认值: <s:property value="admin" default="默认值" /><br />
29.
解析HTML字符串: <s:property value="'<font color=\"red\">Red</font>'" escape="false" /><br />
30.
<s:debug></s:debug>
31.
</body>
32.
</html>
本文介绍Struts2框架中property标签的基本用法,包括显示Action中的属性值、显示字符串、设置默认值及解析HTML字符串等。通过具体示例代码展示了如何配置web.xml、struts.xml文件,并实现UserAction类与JSP页面的交互。
374

被折叠的 条评论
为什么被折叠?



