0. 前言
错误处理是开发中一项很重要的步骤,当用户使用了错误的操作时,系统需要返回能够给用户看得懂的提示,这样开发的产品对用户比较友好。
在学习Struts错误处理前,需要学习资源文件的编写,因为错误处理中的错误信息,都是放在资源文件中保存的。
1.资源文件
1.1 在struts-config.xml中配置资源文件
<message-resources parameter="Action.ApplicationResources" />
这里的Action是我的包名,ApplicationResources是我的资源文件的名字。
1.2 在ApplicationResources.properties中配置资源文件
#这是资源文件,请选择properties。将编码方式选为UTF-8,不然中文无法输入。
info.input.account=Please input account :
1.3 在jsp页面使用资源文件
<html:form action="/login">
<bean:message key="info.input.account"/>
<html:text property="account"></html:text>
<html:submit/><html:cancel/>
login需要在struts-config.xml配置一下,ActionForm因为有表单的存在,所以需要写成JavaBean风格的。
1.4 如何用资源文件显示中文
资源文件里面放置中文是无法被显示出来的,需要手动转码,这样非常麻烦。但是eclipse的插件市场可以帮助解决这个问题。
查找resourceBundle editor,选择install
选择资源文件,右键openWith——资源管理器打开
选择资源,创建中文的资源文件
插件会自动帮我们创建一个资源文件,名字也是新的,去struts-config.xml文件里面配置即可。
2.资源文件配合JSP
资源文件传递固定的字符,JSP来补充另外一半。这就需要用到参数了。
ApplicationResources.properties:
请输入{0} :
这里的{0}就是参数
login.jsp:
<bean:message key="info.input.account" arg0="账号"/>
3.Struts错误处理
前面介绍了资源文件,Struts错误处理就是利用资源文件进行处理的。
3.1 场景
假设要求输入的账号密码不能为空、长度必须在6~10之间、名字为oushile的人被拉入了黑名单,不允许登录
3.2 实现
配置sturts-config.xml:
/login 需要加上input属性作为错误处理。
<form-bean name="loginForm" type="Action.LoginForm"></form-bean>
<action input="/login.jsp" name="loginForm" path="/login" type="Action.LoginAction"></action>
LoginAction.java:
package Action;
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;
/**
* 作用:
*@author 欧世乐
*/
public class LoginForm extends ActionForm{
private String account,password;
/**
* @return the account
*/
public String getAccount() {
return account;
}
/**
* @param account the account to set
*/
public void setAccount(String account) {
this.account = account;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors=new ActionErrors();//错误集合,专门容纳ActionError。
if(account.length()==0) {
ActionMessage error=new ActionMessage("error.null","账号");//第一个参数是消息内容key
//第二个参数是取代{0}
errors.add("account",error);//第一个参数是对这个属性进行标记
}
else if(password.length()==0) {
ActionMessage error=new ActionMessage("error.null","密码");
errors.add("password",error);
}
else if(account.length()>10||account.length()<6) {
ActionMessage error=new ActionMessage("error.length","账号","6","10");
errors.add("length",error);
}
return errors;
}
}
需要重写方法validate,ActionErrors是专门容纳前段错误的类,ActionMessage类可以发送错误信息和参数。errors.add方法可以提交错误类型,在JSP页面使用html:errors property=“错误名称” /即可。
ApplicationResources.properties:
error.length ={0}不可以小于{1}或者大于{2}
error.null={0}不可为空
这里要用前面提到的资源文件编辑器进行编写,
乱码是正常的,在“资源”里面打开就行了
login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<html:form action="/login">
<bean:message key="info.input.account" arg0="账号"/>
<html:text property="account" />
<html:errors property="account" />
<html:errors property="length" />
<br>
<bean:message key="info.input.account" arg0="密码"/>
<html:text property="password"></html:text>
<html:errors property="password" />
<br>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
3.3 演示
这里的密码我也使用了文本框,实际操作中应该使用密码框加强安全性。
3.4 黑名单
黑名单一般放在Action里面去做,因为需要调用ActionForm的数据,同时多了一步,需要把error存起来。
LoginAction.java:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
LoginForm loginForm=(LoginForm)form;
String account=loginForm.getAccount();
if(account.equals("oushile")) {
System.out.println("oushile");
ActionMessages errors = new ActionMessages();
ActionMessage error = new ActionMessage("error.blackList",account);
errors.add("black",error);//第一个参数是对这个error加一个属性标记
this.saveErrors(request, errors); //将errors保存起来
return mapping.getInputForward();
}
return null;
4.小结
随着学习的深入,逐渐体会到Struts框架的魅力,在掌握用法的同时也领会到其底层的技术和原理,这是大学课堂里很难学到的宝贵体会。