struts.properties 파일에 uploads.directory라는 값이 정의되어 있다고 가정 해보십시오. 어떻게 프로그램 컨텍스트에서 그 값에 접근 할 수 있습니까?Struts2 값을 프로그래밍 방식으로 .properties 파일에서 가져 오는 방법은 무엇입니까?
답변
당신은 당신에게 속성 값을 반환 gettext에 ("some.property.name")를 사용할 수 있습니다 당신은 패키지의 my.properties 파일 또는 my_locale.propeties 파일을 둘 필요가
http://struts.apache.org/maven/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html
그 당신의 행동 수업을 제공합니다.
클래스 경로에 있어야하는 예제 ApplicationResources.properties
또는 my.properties
의 경우 struts.properties가 아닌 속성 파일에 값을 넣어야합니다. struts.properties 파일 등
당신이 속성은 사용자 정의 된 메시지 파일을 다음과 같은 속성을 추가 할 필요가있다 만든 후 struts.properties에서해야 할 일이 예를 struts.i18n.encoding=UTF-8
또는 struts.devMode = false
에 대한 스트럿에게 특정 속성을로드하는 데 사용됩니다 struts.properties에서 당신이 예를 들어 쉼표로 분리하여 추가해야 다음 하나 이상의 사용자 정의 메시지 속성 파일이있는 경우
struts.custom.i18n.resources=ApplicationResources
파일 :
struts.custom.i18n.resources=ApplicationResources,my
그런 다음 Action 클래스에서는이 같은 메시지 리소스 파일에서 값을 얻을 수 있습니다 getText('propertyName')
를 사용하여 속성 값에 액세스 할 수 있습니다
public class MyAction extends ActionSupport {
public String getUserDetails() {
if("First Name".equals(getText("label.firstName"))) {
System.out.println("In if block");
}
}
}
당신은 또한 더 많은 정보를 얻을 수 .properties
파일에서 값을 가져 오는 방법 자바 클래스 또는 JSP 파일. JSP에 대한 :
<s:text name="label.firstName" />
및
<s:property value="getText('label.age')" />
은이 링크를 통해 이동할 수 있습니다 자세한 내용은 :
get info here
는 ActionSupport
개체 만들기 및 ActionSupport
클래스의 getText()
방법을 사용하여.
ActionSupport actionSupport = new ActionSupport();
actionSupport.getText("foo.bar");
src
아래에 resources 폴더를 만듭니다. struts.xml
파일에서 상수를 추가하십시오 (예 : <constant name="struts.custom.i18n.resources" value="global"></constant>
). 여기에서 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>
<%@ 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>
세계적인 위해 welcome.jsp.속성 위의 링크가 현재 작동하지 않는 액션 클래스
System.out.println(getText("action.welcome.username"));
에서
. –
@MandarPandit 링크가 http://struts.apache.org/maven/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html로 업데이트되었습니다. – timbru31
업데이트 해 주셔서 감사합니다. :) –