论坛首页 入门技术论坛

支持国际化

浏览 1508 次
锁定老帖子 主题:支持国际化
该帖已经被评为新手帖
作者 正文
   发表时间:2008-07-19  
有关java中支持国际化的解释不用多说了,一搜索就晓得了。下面介绍个小例子。希望能对它不了解的人有所帮助。

首先建立三个jsp。  login.jsp   success.jsp  errors.jsp
login.jsp中
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<body>
<p>&nbsp;</p>
<form id="form1" name="form1" method="post" action="login.do">
  用户名
  <label>
  <input name="username" type="text" id="username" />
  </label>
<html:errors property="a1"/><p>密码
    <label>
    <input name="password" type="text" id="password" />
    </label>
   <html:errors property="a2"/></p>
  <p>
    <label>
    <input type="submit" name="Submit" value="提交" />
    </label>

  </p>
</form>

</body>
</html>



其中需要注意的是在jsp中引用<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> 还有<html:errors property="a1"/>

Success.jsp里面写上登录成功  errors.jsp 里面写上登录失败就ok了。


建立from

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/** 
 * MyEclipse Struts
 * Creation date: 05-26-2008
 * 
 * XDoclet definition:
 * @struts.form name="loginForm"
 */
public class LoginForm extends ActionForm {
	/*
	 * Generated fields
	 */

	/** password property */
	private String password;

	/** username property */
	private String username;

	/*
	 * Generated Methods
	 */

	/** 
	 * Method validate
	 * @param mapping
	 * @param request
	 * @return ActionErrors
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		ActionErrors errors=new ActionErrors();
		if(username.length()<1)//这里是判断用户名不能为空
			errors.add("a1",new ActionMessage("user.null"));
		
		if(password.length()<1)//这里是判断密码不能为空
			errors.add("a2",new ActionMessage("pwd.null"));

		
		return errors;
		
	}

	/** 
	 * Method reset
	 * @param mapping
	 * @param request
	 */
	
	
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// TODO Auto-generated method stub
	}

	/** 
	 * Returns the password.
	 * @return String
	 */
	public String getPassword() {
		return password;
	}

	/** 
	 * Set the password.
	 * @param password The password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/** 
	 * Returns the username.
	 * @return String
	 */
	public String getUsername() {
		return username;
	}

	/** 
	 * Set the username.
	 * @param username The username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}
}



建立action

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import form.LoginForm;


/** 
 * MyEclipse Struts
 * Creation date: 05-26-2008
 * 
 * XDoclet definition:
 * @struts.action path="/login" name="loginForm" scope="request" validate="true"
 * @struts.action-forward name="ok" path="/success.jsp"
 * @struts.action-forward name="nook" path="/error.jsp"
 */
public class LoginAction extends Action {
	/*
	 * Generated Methods
	 */

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
		
		String username=(loginForm).getUsername();
		String password=(loginForm).getPassword();
		if (username.equals("张三")&&password.equals("1")){
			return mapping.findForward("ok");
			
		}
		return mapping.findForward("nook");
	}
}




在dos中  运行 C:\Java\jdk1.5.0_06\bin 中的native2ascii.exe 程序 比如写成1.txt  2.txt
在ApplicationResources.properties中
# Resources for parameter 'com.yourcompany.struts.ApplicationResources'
# Project Test1
user.null=\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a<br>
pwd.null=\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a<br>

大家试试看吧。
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics