Eclipse+Struts2+Spring+MyBatis环境搭建(4)

2019-03-16 17:51

… 3)

struts2 /* 重启Tomcat服务,观察输出的日志,当发现启动日志中包含有“org.apache.struts2”关键字,且无异常抛出,说明struts2的装载过程正常。

编写一个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的配置文件中,该文件同样可以从例程工程

中拷贝过来简单修改: /main.jsp /login.jsp 注意:此时的action实例还是由struts自己来维护,class取值包含完整的路径。

3) 新建两个JSP页面:login.jsp(登陆页面)、main.jsp(登陆成功页面,提示用户登陆成功).

流程说明:用户输入用户名和密码,必须是admin/1234才给验证通过转到main.jsp页面,否则仍然返回到login.jsp页面。 login.jsp

<%@pagelanguage=\contentType=\charset=UTF-8\pageEncoding=\%> <%@taglibprefix=%uri=\%>

Transitional//EN\\>

Insert title here

username:
password:

※注意:标红的部分容易出错,namespace都带上相同的取值,s:form的action上不带.action.

main.jsp <%@pagelanguage=\contentType=\pageEncoding=\%> Insert title here 用户:${requestScope.username },欢迎您登陆 4) 在浏览器中敲入http://localhost:8080/ems/login.jsp进行验证.

增加异常处理机制

采用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) {


Eclipse+Struts2+Spring+MyBatis环境搭建(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:德威工程9月9日安全日活动总结

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: