… 3)
编写一个action验证配置正确性
1) 在src下新建action包:com.jsdz.action,包中新建测试action类,该类继承至ActionSupport,
命名为:LoginAction,用作登陆跳转: packagecom.jsdz.action; import com.opensymphony.xwork2.ActionSupport; publicclassLoginActionextendsActionSupport { private String username ; private String password ; @Override public String execute() throws Exception { System.out.println(\);
if(username.equals(\) &&password.equals(\) ) {
System.out.println(\);
returnSUCCESS; } else {
System.out.println(\); returnINPUT ; } }
public String getUsername() { returnusername; }
publicvoidsetUsername(String username) { this.username = username; }
public String getPassword() { returnpassword; }
publicvoidsetPassword(String password) { this.password = password; } }
2) 在将LoginAction类配置到struts2的配置文件中,该文件同样可以从例程工程
中拷贝过来简单修改:
3) 新建两个JSP页面:login.jsp(登陆页面)、main.jsp(登陆成功页面,提示用户登陆成功).
流程说明:用户输入用户名和密码,必须是admin/1234才给验证通过转到main.jsp页面,否则仍然返回到login.jsp页面。 login.jsp
<%@pagelanguage=\contentType=\charset=UTF-8\pageEncoding=\%> <%@taglibprefix=%uri=\%>
Transitional//EN\\>
username:
password:
※注意:标红的部分容易出错,namespace都带上相同的取值,s:form的action上不带.action.
main.jsp <%@pagelanguage=\contentType=\pageEncoding=\%>
增加异常处理机制
采用struts2自有的异常处理机制,避免所有地方加上try/catch。
1) 增加包com.jsdz.exception,并在其中增加自定义业务异常类BusinessException.java。
packagecom.jsdz.exception;
publicclassBusinessExceptionextendsRuntimeException {
privatestaticfinallongserialVersionUID = 0xc1a865c45ffdc5f9L;
publicBusinessException(String frdMessage) { super(createFriendlyErrMsg(frdMessage)); }
publicBusinessException(Throwablethrowable) { super(throwable); }
publicBusinessException(Throwablethrowable, String frdMessage) { super(throwable); }
privatestatic String createFriendlyErrMsg(String msgBody) { String prefixStr = \抱歉,\;
String suffixStr = \请稍后再试或与管理员联系!\; StringBufferfriendlyErrMsg = newStringBuffer(\); friendlyErrMsg.append(prefixStr); friendlyErrMsg.append(msgBody); friendlyErrMsg.append(suffixStr); returnfriendlyErrMsg.toString(); } }
2) 增加包com.jsdz. interceptor,增加一个异常转化的拦截器类:BusinessInterceptor.java。 packagecom.jsdz.interceptor;
importjava.io.IOException; importjava.sql.SQLException;
importcom.jsdz.exception.BusinessException;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
publicclassBusinessInterceptorextendsAbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println(\);
before(invocation);
String result = \;
try {
result = invocation.invoke(); } catch (DataAccessException ex) {
throw new BusinessException(\数据库操作失败!\; } catch (NullPointerException ex) {
thrownewBusinessException(\调用了未经初始化的对象或者是不存在的对象!\); } catch (IOException ex) {
thrownewBusinessException(\异常!\); } catch (ClassNotFoundException ex) {
thrownewBusinessException(\指定的类不存在!\); } catch (ArithmeticException ex) {
thrownewBusinessException(\数学运算异常!\); } catch (ArrayIndexOutOfBoundsException ex) { thrownewBusinessException(\数组下标越界!\); } catch (IllegalArgumentException ex) {
thrownewBusinessException(\方法的参数错误!\); } catch (ClassCastException ex) {
thrownewBusinessException(\类型强制转换错误!\); } catch (SecurityException ex) {
thrownewBusinessException(\违背安全原则异常!\); } catch (SQLException ex) {
thrownewBusinessException(\操作数据库异常!\); } catch (NoSuchMethodError ex) {
thrownewBusinessException(\方法末找到异常!\); } catch (InternalError ex) {