- 浏览: 160737 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
acang84n:
难得是2.3的版本注解方式不一样!?
Struts2 注解配置 demo2 -
acang84n:
为什么用struts2-convention-plugin-2 ...
Struts2 注解配置 demo2 -
taney_911:
这个正则表达式也太长了把。。。
Java中正则表达式验证日期格式 -
iamaj2eeprogrammer:
非常感谢你给出的例子!
Struts2 注解配置 demo2 -
superran_sy:
害人。。。
8888-88-88
居然也格式正确
Java中正则表达式验证日期格式
转自处:http://www.vaannila.com/struts-2/struts-2-example/struts-2-ui-tags-example-1.html
搭建好struts2环境
web.xml配置
RegisterAction.java类
Country.java类
struts.xml配置
jsp文件
index.jsp
register.jsp
success.jsp
发布到tomcat后在浏览器中输入
http://localhost:8080/Struts2_UITagsExample/
或
http://localhost:8080/Struts2_UITagsExample/populateRegister.action
即可
附件是完整工程
搭建好struts2环境
web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Struts2_UITagsExample</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
RegisterAction.java类
package com.test; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport { private String userName; private String password; private String gender; private String about; private String country; private List<Country> countryList; private String[] community; private List<String> communityList; private Boolean mailingList; public String populate() { countryList = new ArrayList<Country>(); countryList.add(new Country(1, "India")); countryList.add(new Country(2, "USA")); countryList.add(new Country(3, "France")); communityList = new ArrayList<String>(); communityList.add("Java"); communityList.add(".Net"); communityList.add("SOA"); community = new String[] { "Java", ".Net" }; mailingList = true; return "input"; } public String execute() { return SUCCESS; } 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 getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getAbout() { return about; } public void setAbout(String about) { this.about = about; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public List<Country> getCountryList() { return countryList; } public void setCountryList(List<Country> countryList) { this.countryList = countryList; } public String[] getCommunity() { return community; } public void setCommunity(String[] community) { this.community = community; } public List<String> getCommunityList() { return communityList; } public void setCommunityList(List<String> communityList) { this.communityList = communityList; } public Boolean getMailingList() { return mailingList; } public void setMailingList(Boolean mailingList) { this.mailingList = mailingList; } }
Country.java类
package com.test; public class Country { private int countryId; private String countryName; public Country(int id,String name) { countryId = id; countryName = name; } public int getCountryId() { return countryId; } public void setCountryId(int countryId) { this.countryId = countryId; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } }
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> <include file="struts-default.xml"/> <package name="com.test" extends="struts-default"> <action name="*Register" method="{1}" class="com.test.RegisterAction"> <result name="populate">/register.jsp</result> <result name="input">/register.jsp</result> <result name="success">/success.jsp</result> </action> </package> </struts>
jsp文件
index.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <META HTTP-EQUIV="Refresh" CONTENT="0;URL=populateRegister.action"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> </body> </html>
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Register Page</title> </head> <body> <s:form action="Register"> <s:textfield name="userName" label="User Name" /> <s:password name="password" label="Password" /> <s:radio name="gender" label="Gender" list="{'Male','Female'}" /> <s:select name="country" list="countryList" listKey="countryId" listValue="countryName" headerKey="0" headerValue="Country" label="Select a country" /> <s:textarea name="about" label="About You" /> <s:checkboxlist list="communityList" name="community" label="Community" /> <s:checkbox name="mailingList" label="Would you like to join our mailing list?" /> <s:submit /> </s:form> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Details Page</title> </head> <body> User Name: <s:property value="userName" /> <br> Gender: <s:property value="gender" /> <br> Country: <s:property value="country" /> <br> About You: <s:property value="about" /> <br> Community: <s:property value="community" /> <br> Mailing List: <s:property value="mailingList" /> </body> </html>
发布到tomcat后在浏览器中输入
http://localhost:8080/Struts2_UITagsExample/
或
http://localhost:8080/Struts2_UITagsExample/populateRegister.action
即可
附件是完整工程
- Struts2_UITagsExample.rar (3.6 MB)
- 下载次数: 40
发表评论
-
java 图片压缩
2013-10-17 14:30 568http://fandayrockworld.iteye.co ... -
java的spring的quartz 时间配置相应字符串的说明与实例
2011-12-15 11:19 1018本文源自:http://js.sse6.cn/gzsgfrz/ ... -
Spring事务配置的五种方式
2011-01-19 09:12 781http://www.blogjava.net/robbie/ ... -
log4j.xml配置 分级打印日志和打印到指定文件中
2009-11-26 12:50 4319<?xml version="1.0&qu ... -
Spring启动时出现 ERR=12505 异常
2009-11-26 12:47 1558这是由于,spring配置文件中有数据源配置错误,修改后就可以 ... -
用正则表达式替换手机号码前缀86
2009-11-25 09:24 3936Pattern p2 = Patt ... -
ibatis sqlMapConfig settings 中属性用法
2009-09-28 14:12 5119转自 http://zhangzuanqian.iteye.c ... -
SqlMapException: There is no statement named解决方法
2009-09-28 12:51 5501报如下异常解决方法 sqlMapConfig 里加入 < ... -
关于struts2无法加载struts.properties的问题
2009-09-25 17:52 1362http://blog.chinaunix.net/u/200 ... -
Spring2.5、Struts2、Ibatis2.3开发框架搭建
2009-09-25 17:47 1893Spring2.5、Struts2、Ibatis ... -
Struts2 注解配置 demo2
2009-08-21 17:02 6504http://www.vaannila.com/struts- ... -
Struts2 注解配置 demo1
2009-08-21 16:25 1773from http://www.vaannila.com/st ... -
Struts2 HelloWorld MAX的struts教程的demo
2009-08-20 15:51 1137原文http://www.blogjava.net/max/c ... -
使用Struts2
2009-08-20 14:45 1112原文http://developer.51cto.co ... -
JSP乱码解决 之 pageEncoding和contentType属性,UTF-8 GBK gb2312 详解
2009-07-30 10:14 1503文章来源于志伟教程资 ... -
Tomcat的JNDI配置
2009-07-06 15:53 1494驱动需要拷到tomcat/common/lib/目录 1 we ... -
web应用 xml方式配置
2009-07-06 15:49 780编写一个xml文件,然后放到tomcat目录/conf/Cat ... -
Tomcat类装载器
2009-07-06 15:12 835context.xml: <Context> ... -
解决request.getParameter()中文乱码的问题
2009-02-21 16:52 4990jsp页面中 用click事件调用js 此时的页面编码要用UT ... -
搭建SVN服务端
2009-02-09 11:12 1276转载http://hi.baidu.com/axhack/bl ...
相关推荐
Struts2支持应用的多语言支持和UI主题定制,使得应用更易国际化和美化。 8. ** strut2-tags** 文件夹: 可能包含了演示Struts2 JSP标签的示例代码,帮助开发者理解如何在实际项目中使用这些标签。 通过学习和...
import org.apache.struts2.views.jsp.ui.TagSupport; public class CustomTag extends TagSupport { private String attribute1; private String attribute2; // getter & setter @Override public int ...
下面我们将从Struts1.x与Struts2.0标签库的对比入手,再深入探讨Struts2中的非UI标签。 ##### 2.1 Struts1.x与Struts2.0 标签库对比 - **分类**:Struts1.x的标签库按照功能划分为HTML、Tiles、Logic和Bean等几个...
1. **定义标签接口**:Struts2标签库基于OGNL(Object-Graph Navigation Language)表达式,因此我们需要继承`org.apache.struts2.views.jsp.TagSupport`或`org.apache.struts2.views.jsp.ui.Tag`类来创建自定义标签...
2. **创建标签处理类(Tag Handler Class)**:标签处理类是实现自定义标签逻辑的核心,它需要继承自Struts2提供的基类,如`org.apache.struts2.views.jsp.ui.TagSupport`。在该类中,我们需要重写父类的方法来处理...
<%@ taglib prefix="s" uri="/struts-tags" %> ``` 这行代码指定了 Struts2 标签库的前缀为 "s",并且指定了 Struts2 标签库的 URI。 #### 三、条件判断标签 ##### 3.1 `<s:if>` 标签 - **描述**:用于执行基本...
在`src`目录下,创建一个新的包,例如`com.example.struts2.customtags`,用于存放自定义标签的相关类。我们需要一个继承自`org.apache.struts2.views.jsp.ui.TagSupport`的类,如`PagingTag`,用于处理分页逻辑。 ...
- **定义**:Struts2还提供了一系列UI组件标签,如下拉列表、复选框等,这些标签可以帮助开发者快速构建复杂的用户界面。 - **示例代码**: ```jsp <%@ taglib prefix="s" uri="/struts-tags" %> ,'USA','...
Struts提供了丰富的标签库(Tiles、Struts Tags等),使得开发者可以方便地在JSP页面中构建UI,同时通过ActionForm对象收集用户输入,实现数据验证,确保数据安全。例如,`<s:textfield>`标签用于创建表单字段,`...
1. **定义标签接口**:通常我们会继承`org.apache.struts2.views.jsp.ui.TagSupport`,它是Struts2提供的基础标签类,包含了处理标签的基本方法。 2. **实现标签逻辑**:在自定义标签类中,我们需要实现获取下拉框...