[quote]ApplicationResources.properties使用,让开发显示变得更灵活。
做过Struts国际化的就知道这里的应用会使得,标签、按钮的显示更为灵活,将标签、按钮中的文字通过ApplicationResurces.properties剥离出来,类似配置文件的作用,从而把显示变得更为灵活。[/quote]
使用ApplicationResources.properties举例。
一.安装好Propertise Edits(参看前一篇Propertise Edits安装与使用)。
二.为新建的项目添加Struts应用。
三.用PropertiseEditor打开Applicationresurces.properties,写入想要添加的内容。本例内容如下:
四.在需要的地方使用:
如在Login.jsp中使用:
在Action中的错误信息中使用:
在Form中的验证中使用:
附件:完整代码(运行时需要自行加入Struts应用)
做过Struts国际化的就知道这里的应用会使得,标签、按钮的显示更为灵活,将标签、按钮中的文字通过ApplicationResurces.properties剥离出来,类似配置文件的作用,从而把显示变得更为灵活。[/quote]
使用ApplicationResources.properties举例。
一.安装好Propertise Edits(参看前一篇Propertise Edits安装与使用)。
二.为新建的项目添加Struts应用。
三.用PropertiseEditor打开Applicationresurces.properties,写入想要添加的内容。本例内容如下:
# Resources for parameter 'com.struts.ApplicationResources'
# Project LoignApp
login.jsp.title = 我的第一个Struts应用
login.jsp.head = 请您登录
login.jsp.loginNameLabel = 登录帐号
login.jsp.passwordLabel = 登录密码
login.jsp.submit = 登录
login.jsp.cancel = 取消
login.jsp.noLoginName = 登录帐号不能为空!<br>
login.jsp.noPassword = 登录密码不能为空!<br>
login.jsp.notExist = 对不起,该用户不存在!<br>
index.jsp.welcome = 欢迎进入Struts世界!四.在需要的地方使用:
如在Login.jsp中使用:
<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>
<bean:message key="login.jsp.title"/>
</title>
</head>
<body>
<h2><bean:message key="login.jsp.head"/></h2>
<html:form action="/login">
<bean:message key="login.jsp.loginNameLabel"/>
<html:text property="username"/><html:errors property="username"/><br/>
<bean:message key="login.jsp.passwordLabel"/>
<html:password property="password"/><html:errors property="password"/><br/>
<html:submit>
<bean:message key="login.jsp.submit"/>
</html:submit>
<html:cancel>
<bean:message key="login.jsp.cancel"/>
</html:cancel><br/>
<html:errors/>//在JSP页面中加入错误提示
</html:form>
</body>
</html>
在Action中的错误信息中使用:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.struts.form.LoginForm;
import com.struts.model.LoginCheck;
public class LoginAction extends Action {
@SuppressWarnings("deprecation")
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm lf = (LoginForm) form;
String loginName=lf.getUsername();
String pwd=lf.getPassword();
ActionErrors errors=new ActionErrors();//新建错误提示
LoginCheck lc=new LoginCheck();
boolean result=lc.loginChk(loginName, pwd);
if(result==true){
return mapping.findForward("s");
}else{
errors.add("noPassword",new ActionError("login.jsp.noPassword"));//New一个ActionError(注意这里没有“s”)将错误提示加入到Errors中。
// saveErrors(request,errors);
return mapping.findForward("f");
}
}
}在Form中的验证中使用:
..........
@SuppressWarnings("deprecation")
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
[color=red]ActionErrors errors=new ActionErrors();[/color] if((username==null||username.length()<1)){
[color=red]errors.add("noLoginName",new ActionError("login.jsp.noLoginName"));[/color]
}
if((password==null||password.length()<1)){
errors.add("noPassword",new ActionError("login.jsp.noPassword"));
}
return errors;
}
........
附件:完整代码(运行时需要自行加入Struts应用)
本文介绍如何使用ApplicationResources.properties文件实现Struts应用的国际化,包括配置文件的编辑、JSP页面与Action中消息资源的调用及表单验证错误信息的设置。
2168

被折叠的 条评论
为什么被折叠?



