如何以编程方式从 .properties 文件获取 Struts2 值?

发布于 2024-07-16 02:39:38 字数 89 浏览 11 评论 0原文

假设我有一个 struts.properties 文件,其定义值 uploads.directory 。 如何以编程方式从 Actioncontext 访问该值?

Say I have a struts.properties file with a defined value uploads.directory . How can I access that value from an Actioncontext programatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

愁以何悠 2024-07-23 02:39:39

You can use getText("some.property.name") which return you the property value

http://struts.apache.org/maven/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html

墨小沫ゞ 2024-07-23 02:39:39

创建 ActionSupport 对象并使用 ActionSupport 类的 getText() 方法。

ActionSupport actionSupport = new ActionSupport();
actionSupport.getText("foo.bar");

Create ActionSupport Object and by using getText() method of ActionSupport class.

ActionSupport actionSupport = new ActionSupport();
actionSupport.getText("foo.bar");
赏烟花じ飞满天 2024-07-23 02:39:39

src下创建一个资源文件夹。
struts.xml 文件中添加一个常量,例如
这里的global是属性文件的名称。
现在您将能够在整个应用程序中使用这些属性。

<?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>

welcome.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><s:property value="getText('action.welcome.title')"/></title>
    </head>
    <body>
             <s:property value="getText('action.welcome.username')"/>: <s:property value="username"/><br>
    </body>
    </html>

global.properties

action.welcome.username=waqar

操作类中的

System.out.println(getText("action.welcome.username"));

Create a resources folder under src.
In the struts.xml file add a constant e.g., <constant name="struts.custom.i18n.resources" value="global"></constant>
Here global is the name of properties file.
Now you will be able to use the properties in the entire application.

<?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>

The welcome.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><s:property value="getText('action.welcome.title')"/></title>
    </head>
    <body>
             <s:property value="getText('action.welcome.username')"/>: <s:property value="username"/><br>
    </body>
    </html>

global.properties

action.welcome.username=waqar

In action class

System.out.println(getText("action.welcome.username"));
慕巷 2024-07-23 02:39:39

您需要将 my.properties 文件或 my_locale.propeties 文件放入包含操作类的包中。

You need to put the my.properties file or my_locale.propeties file in the package that houses your action class.

2024-07-23 02:39:39

您需要将值放在 struts.properties 以外的属性文件中,例如需要位于类路径中的 ApplicationResources.propertiesmy.properties 。 struts.properties 文件用于加载 struts 特定属性,例如 struts.i18n.encoding=UTF-8struts.devMode = false 等。

您需要做的事情在为自定义消息创建属性文件后,您必须在 struts.properties 文件中添加以下属性

struts.custom.i18n.resources=ApplicationResources

如果您有多个自定义消息属性文件,则需要通过用逗号分隔来添加它们,例如:

struts.custom.i18n.resources=ApplicationResources,my

然后,在您的操作类中,您可以使用 getText('propertyName') 访问属性值

You need to put the values in properties files other than struts.properties for examples ApplicationResources.properties or my.properties which needs to be in the classpath. struts.properties file is used to load struts specific properties for example struts.i18n.encoding=UTF-8 or struts.devMode = false etc.

The thing you need to do in struts.properties after you create the properties file for your customized messages is you have to add the following property in struts.properties file

struts.custom.i18n.resources=ApplicationResources

If you have more than one custom message property files then you need to add them by separating with comma for example:

struts.custom.i18n.resources=ApplicationResources,my

Then in your action classes you can access the property values by using getText('propertyName')

幸福不弃 2024-07-23 02:39:39

您可以像这样从消息资源文件中获取值:

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:

<s:text name="label.firstName" />

有关

<s:property value="getText('label.age')" />

更多信息,您可以访问此链接:
在此处获取信息

you can get value from message resource file like this:

public class MyAction extends ActionSupport {

   public String getUserDetails() {
      if("First Name".equals(getText("label.firstName"))) {
          System.out.println("In if block");
      }
   }
}

you can also get more information, how to get values from .properties files in java class or jsp files.
for JSP:

<s:text name="label.firstName" />

and

<s:property value="getText('label.age')" />

for more information you can go through this link:
get info here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文