日期:2014-05-16  浏览次数:20468 次

struts标签+jstl标签之国际化实例

      Struts提供了国际化的功能,对于一个面向各国的系统来说,是非常有帮助的。只需要提供每个国家的语言资源包,配置后即可使用。


      下面来用一个登录实例来演示一下Struts的国际化配置和显示。


      创建一个login_i18n_exception的javaweb项目,引入Struts的所有jar包以及jstl.jar和standard.jar。登录界面无非就是输入用户名,密码,所以ActionForm中只需要设置2个属性即可。

package com.bjpowernode.struts;

import org.apache.struts.action.ActionForm;

/**
 * 登录ActionForm,负责收集表单数据
 * 表单的数据必须和ActionForm的get,set一致
 * @author Longxuan
 *
 */
@SuppressWarnings("serial")
public class LoginActionForm extends ActionForm {

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

      登录时会验证密码是否正确,需要提供异常处理,本实例显示2个异常:用户名未找到,密码错误。

package com.bjpowernode.struts;
/**
 * 密码错误异常
 * @author Longxuan
 *
 */
@SuppressWarnings("serial")
public class PasswordErrorException extends RuntimeException {

	public PasswordErrorException() {
		// TODO Auto-generated constructor stub
	}

	public PasswordErrorException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}

	public PasswordErrorException(Throwable cause) {
		super(cause);
		// TODO Auto-generated constructor stub
	}

	public PasswordErrorException(String message, Throwable cause) {
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

	public PasswordErrorException(String message, Throwable cause,
			boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
		// TODO Auto-generated constructor stub
	}

}



package com.bjpowernode.struts;
/**
 * 用户未找到异常
 * @author Longxuan
 *
 */
@SuppressWarnings("serial")
public class UserNotFoundException extends RuntimeException {

	public UserNotFoundException() {
		// TODO Auto-generated constructor stub
	}

	public UserNotFoundException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}

	public UserNotFoundException(Throwable cause) {
		super(cause);
		// TODO Auto-generated constructor stub
	}

	public UserNotFoundException(String message, Throwable cause) {
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

	public UserNotFoundException(String message, Throwable cause,
			boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
		// TODO Auto-generated constructor stub
	}

}

      提供用户管理类,处理用户的相关操作,这里主要处理用户登录:

package com.bjpowernode.struts;
/**
 * 用户管理类
 * @author Longxuan
 *
 */
public class UserManager {
	
	/**
	 * 简单处理登录逻辑
	 * @param username	用户名
	 * @param password	密码
	 */
	public void login(String username,String password){
		
		if(!"admin".equals(username)){
			throw new UserNotFoundException();
		}
		if(! "admin".equals(password)){
			throw new PasswordErrorException();
		}
	}
}

      现在写LoginAction的处理:

package com.bjpowernode.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Actio