如何通过编程方式从.properties文件中获取Struts2值?
假设我有一个struts.properties文件,其定义值为uploads.directory。如何通过编程方式从Actioncontext访问该值?您可以使用getText(“some.property.name”)返回属性值如何通过编程方式从.properties文件中获取Struts2值?,properties,struts2,actioncontext,Properties,Struts2,Actioncontext,假设我有一个struts.properties文件,其定义值为uploads.directory。如何通过编程方式从Actioncontext访问该值?您可以使用getText(“some.property.name”)返回属性值 您需要将my.properties文件或my_locale.properties文件放在包含action类的包中。您需要将值放在属性文件中,而不是struts.properties文件,例如ApplicationResources.properties或my.prop
您需要将my.properties文件或my_locale.properties文件放在包含action类的包中。您需要将值放在属性文件中,而不是struts.properties文件,例如
ApplicationResources.properties
或my.properties
,它们需要位于类路径中。struts.properties文件用于加载struts特定的属性,例如struts.i18n.encoding=UTF-8
或struts.devMode=false
等
为自定义消息创建属性文件后,需要在struts.properties中执行的操作是,必须在struts.properties文件中添加以下属性
struts.custom.i18n.resources=ApplicationResources
如果您有多个自定义邮件属性文件,则需要使用逗号分隔来添加它们,例如:
struts.custom.i18n.resources=ApplicationResources,my
然后,在动作类中,您可以使用
getText('propertyName')访问属性值您可以从消息资源文件中获得如下值:
public class MyAction extends ActionSupport {
public String getUserDetails() {
if("First Name".equals(getText("label.firstName"))) {
System.out.println("In if block");
}
}
}
您还可以获得更多信息,如如何从java类或jsp文件中的.properties
文件中获取值。
对于JSP:
及
有关更多信息,请访问以下链接:
创建ActionSupport
对象,并使用ActionSupport
类的getText()
方法
ActionSupport actionSupport = new ActionSupport();
actionSupport.getText("foo.bar");
在src
下创建资源文件夹。
在struts.xml
文件中添加一个常量,例如
这里是全局属性文件的名称。
现在,您将能够在整个应用程序中使用这些属性
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- constant to define result path locations to project root directory -->
<!-- constant to define global resource bundle -->
<constant name="struts.custom.i18n.resources" value="global"></constant>
<package name="user" namespace="/" extends="struts-default">
<action name="home">
<result>/home.jsp</result>
</action>
<action name="welcome" class="com.waqar.struts2.actions.WelcomeAction">
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
行动课
System.out.println(getText("action.welcome.username"));
@MandarPandit链接已更新为
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- constant to define result path locations to project root directory -->
<!-- constant to define global resource bundle -->
<constant name="struts.custom.i18n.resources" value="global"></constant>
<package name="user" namespace="/" extends="struts-default">
<action name="home">
<result>/home.jsp</result>
</action>
<action name="welcome" class="com.waqar.struts2.actions.WelcomeAction">
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
<%@ 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><s:property value="getText('action.welcome.title')"/></title>
</head>
<body>
<s:property value="getText('action.welcome.username')"/>: <s:property value="username"/><br>
</body>
</html>
action.welcome.username=waqar
System.out.println(getText("action.welcome.username"));