Struts学习笔记4——支持国际化

有关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> </p>
<form id="form1" name="form1" method="post" action="rr.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


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;

public class LoginForm extends ActionForm {
/*
* Generated fields
*/

/** password property */
private String password;

/** username property */
private String username;


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"));//a1对应的是jsp中的html:errors标签 user.null与消息文件中的相对应。a 2 类似。

if(password.length()<1)//这里是判断密码不能为空
errors.add("a2",new ActionMessage("pwd.null"));


return errors;

}


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


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}
}



建立action


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;

public class LoginAction extends Action {

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("11")&&password.equals("1")){
return mapping.findForward("ok");

}
return mapping.findForward("nook");
}
}


在ApplicationResources.properties中(使用JDK中的自带的native2ascii.exe程序)

# 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>



Struts-config.cml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
<data-sources />
<form-beans >
<form-bean name="adminForm" type="form.AdminForm" />

</form-beans>

<global-exceptions />
<global-forwards />
<action-mappings >

<action
attribute="adminForm"
input="/login.jsp"
name="adminForm"
path="/rr"
scope="request"
type="action.RrAction">
<forward name="ok" path="/success.jsp" />
<forward name="nook" path="/login.jsp" />
</action>

</action-mappings>

<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>


注意 input="/login.jsp" 是把错误信息显示在登录页面

大家试试看吧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值