Struts2屬性文件簡單應用
在包下建立名為pageckage.properties的文件.我這里的包是com.內容如下:
requiredstring = $\{getText(fieldName)} is required.
password = Password
username = User Name
這里我們需要改一下我們的LoginAction-validation.xml:
<message>Username is required</message>
<message key="requiredstring"/>

<message>Password is required</message>
<message key="requiredstring"/>
<%@ page language="java" pageEncoding="GB18030"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>My JSP 'login' starting page</title>
</head>

<body>
<s:form action="login" method="post">
<s:textfield name="username" label="%{getText('username')}" />
<br />
<s:textfield name="password" label="%{getText('password')}" />
<br />
<s:submit />
</s:form>
</body>
</html>
<struts>
<package name="com" extends="struts-default">
<action name="*" class="com.LoginAction">
<result name="input">/login.jsp</result>
<result>/success.jsp</result>
</action>
</package>
</struts>
LoginAction文件:
package com;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{

private static final long serialVersionUID = 4771028725069625041L;
private String username;
private String password;
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;
}
@Override
public String execute()
{
System.out.println(getText("username") + ":" + username);
System.out.println(getText("password") + ":" + password);
return SUCCESS;
}
}



這里我們需要改一下我們的LoginAction-validation.xml:






紅色-號標志的為原先內容,+號內容為現在修改后的內容.
login.jsp:


















struts.xml文件:












































properties文件的內容按以下順序檢索:
Resource bundles are searched in the following order:
- ActionClass.properties
- BaseClass.properties (all the way to Object.properties)
- Interface.properties (every interface and sub-interface)
- ModelDriven's model (if implements ModelDriven), for the model object repeat from 1
- package.properties (of the directory where class is located and every parent directory all the way to the root directory)
- search up the i18n message key hierarchy itself
- global resource properties
源碼請在我的網盤下
posted on 2007-11-24 07:29 々上善若水々 閱讀(1213) 評論(0) 編輯 收藏 所屬分類: Struts2