Struts2 - 在Tag中使用Properties属性文件(从零开始学习Strust2_04)
2012-03-13 00:42
465 查看
开发环境:
Eclipse IDE for Java EE Developers(下载地址)
struts-2.3.1.2(下载地址)
apache-tomcat-6.0.35(下载地址)
结果图:
使用Properties的好处是可以简化Tag标签的一些内容,同时方便灵活配置资源,例如英文界面和中文界面的切换。
属性文件的应用有几点要求:
(1)RegisterAction.properties的名字和RegisterAction.java名字相同
(2)RegisterAction.properties放在和RegisterAction.java同一个包下
(3)使用的这个特性的页面register.jsp必须是通过Action的Result跳转过来的
web.xml文件没有多少变化,只不过启动的页面从register.jsp改为了index.jsp,原因见下面:
struts.xml文件增加了一个Action
这是因为在register.jsp中的tag标签中的属性改用key,为了能够使key属性找到properties文件,register.jsp的显示必须是通过某一个Action的结果(Result)跳转过来的。所以增加了一个了name
= registerInput的Action。
重点来了,index.jsp和register.jsp
register.jsp中的tag标签改用了属性Key而不是name和Label,但是同样可以达到相同的效果。唯一的要求是这个页面必须是通过Action的Result过来的。<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="RegisterAction">
<s:textfield key="userRegInfoBean.account"></s:textfield>
<s:password key="userRegInfoBean.password"></s:password>
<s:password key="userRegInfoBean.pwdValidate"></s:password>
<s:textfield key="userRegInfoBean.realName"></s:textfield>
<s:textfield key="userRegInfoBean.email"></s:textfield>
<s:textfield key="userRegInfoBean.msn"></s:textfield>
<s:textfield key="userRegInfoBean.qq"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
regSuccess.jsp没有变化
RegisterAction.java和UserRegInfo.java没有改变
属性文件Properties的书写
Eclipse插件的下载地址
Eclipse IDE for Java EE Developers(下载地址)
struts-2.3.1.2(下载地址)
apache-tomcat-6.0.35(下载地址)
结果图:
使用Properties的好处是可以简化Tag标签的一些内容,同时方便灵活配置资源,例如英文界面和中文界面的切换。
属性文件的应用有几点要求:
(1)RegisterAction.properties的名字和RegisterAction.java名字相同
(2)RegisterAction.properties放在和RegisterAction.java同一个包下
(3)使用的这个特性的页面register.jsp必须是通过Action的Result跳转过来的
web.xml文件没有多少变化,只不过启动的页面从register.jsp改为了index.jsp,原因见下面:
<?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_20120312_02</display-name> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
struts.xml文件增加了一个Action
这是因为在register.jsp中的tag标签中的属性改用key,为了能够使key属性找到properties文件,register.jsp的显示必须是通过某一个Action的结果(Result)跳转过来的。所以增加了一个了name
= registerInput的Action。
<?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.devMode" value="true" /> <package name="default" extends="struts-default"> <action name="RegisterAction" class="com.zeph.struts2.action.RegisterAction"> <result name="success">/regSuccess.jsp</result> <result name="input">/register.jsp</result> </action> <action name="registerInput" class="com.zeph.struts2.action.RegisterAction" method="input"> <result name="input">/register.jsp</result> </action> </package> </struts>
重点来了,index.jsp和register.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="/struts-tags" prefix="s" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>A action link to Register page to make sure properties can be used</title> </head> <body> <s:url action="registerInput" var="registerInputLink" /> <p> <a href="${registerInputLink}">Please register</a> </p> </body> </html>
register.jsp中的tag标签改用了属性Key而不是name和Label,但是同样可以达到相同的效果。唯一的要求是这个页面必须是通过Action的Result过来的。<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="RegisterAction">
<s:textfield key="userRegInfoBean.account"></s:textfield>
<s:password key="userRegInfoBean.password"></s:password>
<s:password key="userRegInfoBean.pwdValidate"></s:password>
<s:textfield key="userRegInfoBean.realName"></s:textfield>
<s:textfield key="userRegInfoBean.email"></s:textfield>
<s:textfield key="userRegInfoBean.msn"></s:textfield>
<s:textfield key="userRegInfoBean.qq"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
regSuccess.jsp没有变化
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> account: <s:property value="userRegInfoBean.account" /> <br> real name: <s:property value="userRegInfoBean.realName" /> <br> e-mail: <s:property value="userRegInfoBean.email" /> <br> MSN: <s:property value="userRegInfoBean.msn" /> <br> QQ: <s:property value="userRegInfoBean.qq" /> </body> </html>
RegisterAction.java和UserRegInfo.java没有改变
package com.zeph.struts2.action; import com.opensymphony.xwork2.ActionSupport; import com.zeph.struts2.bean.UserRegInfo; public class RegisterAction extends ActionSupport { private static final long serialVersionUID = 1L; private UserRegInfo userRegInfoBean; public UserRegInfo getUserRegInfoBean() { return userRegInfoBean; } public void setUserRegInfoBean(UserRegInfo userRegInfoBean) { this.userRegInfoBean = userRegInfoBean; } @Override public String execute() throws Exception { return SUCCESS; } @Override public void validate() { if (userRegInfoBean.getAccount().length() == 0) { addFieldError("userRegInfoBean.account", "Account is required."); } if (userRegInfoBean.getEmail().length() == 0) { addFieldError("userRegInfoBean.email", "Email is required."); } if (userRegInfoBean.getRealName().length() == 0) { addFieldError("userRegInfoBean.realName", "Real Name is required."); } if (!userRegInfoBean.getPassword().equals( userRegInfoBean.getPwdValidate())) { addFieldError("userRegInfoBean.pwdValidate", "Password input is inconsistent"); } } }
package com.zeph.struts2.bean; public class UserRegInfo { private String account; private String password; private String pwdValidate; private String realName; private String email; private String msn; private String qq; public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPwdValidate() { return pwdValidate; } public void setPwdValidate(String pwdValidate) { this.pwdValidate = pwdValidate; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getMsn() { return msn; } public void setMsn(String msn) { this.msn = msn; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } @Override public String toString() { return "Account:" + getAccount() + "Real Name:" + getRealName() + "E-Mail:" + getEmail() + "MSN:" + getMsn() + "QQ:" + getQq(); } }
属性文件Properties的书写
#Created by JInto - www.guh-software.de #Mon Mar 12 23:45:24 CST 2012 userRegInfoBean.account=Account userRegInfoBean.email=E-Mail userRegInfoBean.msn=MSN userRegInfoBean.password=Password userRegInfoBean.pwdValidate=Input Password Again userRegInfoBean.qq=QQ userRegInfoBean.realName=Real Name我自己使用了一个Eclipse的插件所以上面有注释
Eclipse插件的下载地址
相关文章推荐
- Struts2 - Controll Tag的使用(从零开始学习Strust2_06)
- JSP学习——Properties属性文件的使用注意
- Struts2 - Struts2的文件上传 [单个文件上传](从零开始学习Strust2_09)
- Spring学习笔记 通过PropertyPlaceholderConfigurer来使用properties文件初始化Map类型属性
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- Struts2使用4-- Struts2的配置2-- Struts.properties 属性文件
- Struts2 - 使用XML实现简单Validation(从零开始学习Strust2_05)
- Struts2 - Struts2的文件下载(从零开始学习Strust2_10)
- Java 反射将配置文件数据加载到对象属性中Reflect与Properties使用
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- 使用JAVA读写Properties属性文件
- Spring中属性文件properties的读取与使用
- 8.Spring学习笔记_使用外部属性文件(by尚硅谷_佟刚)
- 从零开始前端学习[47]:如何使用javascript来操作行内样式或者属性浅析
- Spring学习记录(六)---使用外部属性文件
- 使用JAVA读写Properties属性文件
- spring4学习:使用外部属性文件
- Spring4 学习系列之——使用外部属性文件
- struts2学习笔记--struts2中如何处理properties文件中的占位符{0}
- 从零开始学习jQuery (四) 使用jQuery操作元素的属性与样式