搜档网
当前位置:搜档网 › Eclipse+Struts2+Spring+MyBatis环境搭建

Eclipse+Struts2+Spring+MyBatis环境搭建

1.准备安装文件

以下为需要准备的文件清单,从相应网址下载到本地,表格中列出的版本号是截止到2012

年5月6日的最新版本。

5 tomcat 7.0.27 apache-tomcat-7.0.27-windows-x86.zip https://www.sodocs.net/doc/f815702552.html,/

下载包含了库和代码、例子的全包:struts-2.3.3-all.gz,大约在76m,文件较大,但其中的空应用例程可作为直接拷贝的参考。

下载包含了库和代码、例子的全包:spring-framework-3.1.1.RELEASE-with-docs.zip MyBatis

2.新建WEB工程

选择WEB下Dynamic Web Project类型的工程:

工程命名为EMS,其它选项保持默认。

文件夹名保持默认

勾上生成web.xml选项,其它保持默认。

最后得到一个如下组织的WEB工程.

补充:

在Java Resources/src目录上通过右键菜单将JAVA代码下编译输出目录修改至WebContent/WEB-INF/classes目录,与myEclipse的默认方式保持一致,便于实时发布,如果不做这一步class等文件不会自动输出到WEB-INF下。

3.配置Tomcat服务器切换Servers页签:

※建议不采用eclipse的应用发布功能,改用修改Tomcat配置文件的方式来实现发布,因此这里暂时不作选择。

在Eclipse中测试启动Tomcat服务器:

采用修改Tomcat配置的方式来发布应用(建议采用,这是相对快捷的发布

1、双击tomcat服务器,打开配置选项,将Server Location选项置为第二个:

2、用文本编辑器打开:D:\tomcat\apache-tomcat-7.0.27\conf\server.xml文件,在Host节点之上增加虚拟目录描述:

pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>

4、在eclipse中重启tomcat,在浏览器中敲入URL:http://localhost:8080/ems/NewFile.jsp

验证是否看到正确的输出信息。

配置TOMCAT管理界面

打开:D:\tomcat\apache-tomcat-7.0.27\conf\tomcat-users.xml文件,加入:

之后通过admin/admin即可登陆管理界面。

4.配置Structs2

搭建环境

从已下载的struts-2.3.3-all.gz文件中解压出自带的一个空例程:struts2-blank,直接从展开的文件夹中复制内容过来:

1)复制struts2-blank\WEB-INF\lib目录下的所有JAR包到该工程的WEB-INF/lib目录下(注意:

这里指的是空例程里面的JAR包,是一些最基本的包,不能直接把structs2里面的所有的包都一下子导进来,那样会出错的!)

2)打开struts2-blank\WEB-INF\web.xml,复制其中过滤器到该工程相同目录下web.xml,,

让struts2在tomcat启动的时候加载:

struts2

/*

3)重启Tomcat服务,观察输出的日志,当发现启动日志中包含有“org.apache.struts2”关

键字,且无异常抛出,说明struts2的装载过程正常。

编写一个action验证配置正确性

1)在src下新建action包:com.jsdz.action,包中新建测试action类,该类继承至ActionSupport,package com.jsdz.action;

import com.opensymphony.xwork2.ActionSupport;

publicclass LoginAction extends ActionSupport {

private String username ;

private String password ;

@Override

public String execute() throws Exception {

System.out.println("LoginAction execute invoked!");

if(username.equals("admin") &&password.equals("1234") )

{

System.out.println("user ok");

return SUCCESS;

}

else

{

System.out.println("user invalid");

return INPUT ;

}

}

public String getUsername() {

return username;

}

publicvoid setUsername(String username) {

https://www.sodocs.net/doc/f815702552.html,ername = username;

}

public String getPassword() {

return password;

}

publicvoid setPassword(String password) {

this.password = password;

}

}

2)在将LoginAction类配置到struts2的配置文件中,该文件同样可以从例程工程

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

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

login.jsp

<%@page language="java"contentType="text/html;

charset=UTF-8"pageEncoding="UTF-8"%>

<%@taglib prefix="s"uri="/struts-tags"%>

Transitional//EN""https://www.sodocs.net/doc/f815702552.html,/TR/html4/loose.dtd">

Insert title here

username:

password:

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

4)在浏览器中敲入http://localhost:8080/ems/login.jsp进行验证.

增加异常处理机制

采用struts2自有的异常处理机制,避免所有地方加上try/catch。

1)增加包com.jsdz.exception,并在其中增加自定义业务异常类BusinessException.java。package com.jsdz.exception;

publicclass BusinessException extends RuntimeException {

privatestaticfinallong serialVersionUID = 0xc1a865c45ffdc5f9L;

public BusinessException(String frdMessage) {

super(createFriendlyErrMsg(frdMessage));

}

public BusinessException(Throwablethrowable) {

super(throwable);

}

public BusinessException(Throwablethrowable, String frdMessage) { super(throwable);

}

privatestatic String createFriendlyErrMsg(String msgBody) { String prefixStr = "抱歉,";

String suffixStr = " 请稍后再试或与管理员联系!";

StringBufferfriendlyErrMsg = new StringBuffer("");

friendlyErrMsg.append(prefixStr);

friendlyErrMsg.append(msgBody);

friendlyErrMsg.append(suffixStr);

return friendlyErrMsg.toString();

}

}

import java.io.IOException;

import java.sql.SQLException;

import com.jsdz.exception.BusinessException;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

publicclass BusinessInterceptor extends AbstractInterceptor {

@Override

public String intercept(ActionInvocation invocation) throws Exception { System.out.println("BusinessInterceptor intercept() invoked! ");

before(invocation);

String result = "";

try {

result = invocation.invoke();

} catch (DataAccessException ex) {

throw new BusinessException("数据库操作失败!");

} catch (NullPointerException ex) {

thrownew BusinessException("调用了未经初始化的对象或者是不存在的对象!");

} catch (IOException ex) {

thrownew BusinessException("IO异常!");

} catch (ClassNotFoundException ex) {

thrownew BusinessException("指定的类不存在!");

} catch (ArithmeticException ex) {

thrownew BusinessException("数学运算异常!");

} catch (ArrayIndexOutOfBoundsException ex) {

thrownew BusinessException("数组下标越界!");

} catch (IllegalArgumentException ex) {

thrownew BusinessException("方法的参数错误!");

} catch (ClassCastException ex) {

thrownew BusinessException("类型强制转换错误!");

} catch (SecurityException ex) {

thrownew BusinessException("违背安全原则异常!");

} catch (SQLException ex) {

thrownew BusinessException("操作数据库异常!");

} catch (NoSuchMethodError ex) {

thrownew BusinessException("方法末找到异常!");

} catch (InternalError ex) {

相关主题