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" />  

红色-号标志的为原先内容,+号内容为现在修改后的内容.

login.jsp:

<% @ 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.xml文件:

< 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;
 }

}


properties文件的内容按以下顺序检索:

Resource bundles are searched in the following order:

  1. ActionClass.properties
  2. BaseClass.properties (all the way to Object.properties)
  3. Interface.properties (every interface and sub-interface)
  4. ModelDriven's model (if implements ModelDriven), for the model object repeat from 1
  5. package.properties (of the directory where class is located and every parent directory all the way to the root directory)
  6. search up the i18n message key hierarchy itself
  7. global resource properties

源码请在我的网盘下

162767.html

々上善若水々 2007-11-24 07:29 发表评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值