




已阅读5页,还剩45页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1. 准备安装文件以下为需要准备的文件清单,从相应网址下载到本地,表格中列出的版本号是截止到2012年5月6日的最新版本。序号库类型版本文件名下载地址1Struts22.3.3struts-2.3.3-all.gz/2Spring3.1.0spring-framework-3.1.1.RELEASE-with-docs.zip/3Mybatis3.1.1mybatis-3.1.1-bundle.zipmybatis-spring-1.1.1-bundle.zipmybatis-generator-core-1.3.1-bundle.zip/p/mybatis/4jquery1.7.2jquery-1.7.2.js/4EclipseEclipse IDE for Java EE Developers,eclipse-jee-indigo-SR2-win32.zip/downloads/5tomcat7.0.27apache-tomcat-7.0.27-windows-x86.zip/Struts2 下载包含了库和代码、例子的全包:struts-2.3.3-all.gz,大约在76m,文件较大,但其中的空应用例程可作为直接拷贝的参考。Spring下载包含了库和代码、例子的全包:spring-framework-3.1.1.RELEASE-with-docs.zipMyBatis2. 新建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:tomcatapache-tomcat-7.0.27confserver.xml 文件,在Host节点之上增加虚拟目录描述: 3、在WebContent目录下新建一个默认JSP页面:NewFile.jsp,打印一行信息:Hello World!Insert title hereHello World!4、在eclipse中重启tomcat,在浏览器中敲入URL:http:/localhost:8080/ems/NewFile.jsp验证是否看到正确的输出信息。配置TOMCAT管理界面打开:D:tomcatapache-tomcat-7.0.27conftomcat-users.xml文件,加入: 之后通过admin/admin即可登陆管理界面。4. 配置Structs2搭建环境从已下载的struts-2.3.3-all.gz文件中解压出自带的一个空例程:struts2-blank,直接从展开的文件夹中复制内容过来:1) 复制struts2-blankWEB-INFlib目录下的所有JAR包到该工程的WEB-INF/lib目录下2) 打开struts2-blankWEB-INFweb.xml,复制其中过滤器到该工程相同目录下web.xml,让struts2在tomcat启动的时候加载:struts2org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterstruts2/*3) 重启Tomcat服务,观察输出的日志,当发现启动日志中包含有“org.apache.struts2”关键字,且无异常抛出,说明struts2的装载过程正常。编写一个action验证配置正确性1) 在src下新建action包:com.jsdz.action,包中新建测试action类,该类继承至ActionSupport,命名为: LoginAction,用作登陆跳转:package com.jsdz.action;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport private String username ;private String password ; Overridepublic 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;public void setUsername(String username) this.username = username;public String getPassword() return password;public void setPassword(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.jspInsert title here username: password: 注意:标红的部分容易出错,namespace都带上相同的取值,s:form的action上不带.action.main.jspInsert title here用户:$requestScope.username ,欢迎您登陆4) 在浏览器中敲入http:/localhost:8080/ems/login.jsp进行验证.增加异常处理机制采用struts2自有的异常处理机制,避免所有地方加上try/catch。1) 增加包com.jsdz.exception,并在其中增加自定义业务异常类BusinessException.java。package com.jsdz.exception;public class BusinessException extends RuntimeException private static final long serialVersionUID = 0xc1a865c45ffdc5f9L;public BusinessException(String frdMessage) super(createFriendlyErrMsg(frdMessage);public BusinessException(Throwable throwable) super(throwable);public BusinessException(Throwable throwable, String frdMessage) super(throwable);private static String createFriendlyErrMsg(String msgBody) String prefixStr = 抱歉,;String suffixStr = 请稍后再试或与管理员联系!;StringBuffer friendlyErrMsg = new StringBuffer();friendlyErrMsg.append(prefixStr);friendlyErrMsg.append(msgBody);friendlyErrMsg.append(suffixStr);return friendlyErrMsg.toString();2) 增加包com.jsdz. interceptor,增加一个异常转化的拦截器类:BusinessInterceptor.java。package erceptor;import java.io.IOException;import java.sql.SQLException;import com.jsdz.exception.BusinessException;import com.opensymphony.xwork2.ActionInvocation;import erceptor.AbstractInterceptor;public class BusinessInterceptor extends AbstractInterceptor Overridepublic 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) throw new BusinessException(调用了未经初始化的对象或者是不存在的对象!); catch (IOException ex) throw new BusinessException(IO异常!); catch (ClassNotFoundException ex) throw new BusinessException(指定的类不存在!); catch (ArithmeticException ex) throw new BusinessException(数学运算异常!); catch (ArrayIndexOutOfBoundsException ex) throw new BusinessException(数组下标越界!); catch (IllegalArgumentException ex) throw new BusinessException(方法的参数错误!); catch (ClassCastException ex) throw new BusinessException(类型强制转换错误!); catch (SecurityException ex) throw new BusinessException(违背安全原则异常!); catch (SQLException ex) throw new BusinessException(操作数据库异常!); catch (NoSuchMethodError ex) throw new BusinessException(方法末找到异常!); catch (InternalError ex) throw new BusinessException(Java虚拟机发生了内部错误); catch (Exception ex) throw new BusinessException(程序内部错误,操作失败!);after(invocation, result);return result ;/* * 验证登陆等. * param invocation * return * throws Exception */public void before(ActionInvocation invocation) throws Exception /./* * 记录日志等. * param invocation * return * throws Exception */public void after(ActionInvocation invocation,String result) throws Exception/.3) 在WebContent目录下新建error.jsp,代表出错跳转的页面:error页面简单的打印出异常信息。4) 在struts.xml配置文件中加入拦截器及错误跳转指示:/error.jsp/main.jsp/login.jsp5) 在LoginAction的execute方法中故意加入会产生异常的代码,测试页面转向:int i= 10/0 ;结果跳转至error.jsp,并显示“抱歉,数学运算异常! 请稍后再试或与管理员联系!”。5. 整合spring复制struts-spring插件包从struts-2.3.3-all.gz包中复制struts2-spring-plugin-2.3.3.jar、commons-logging-1.1.1.jar两个JAR文件到该工程的WEB-INF/lib目录下:复制springJAR包从spring-framework-3.1.1.RELEASE-with-docs.zip包中复制spring相关的JAR文件(6个)该工程的WEB-INF/lib目录下:修改web.xml在文件中增加spring监听器配置信息,让spring在tomcat启动的时候加载: org.springframework.web.context.ContextLoaderListener org.springframework.web.context.request.RequestContextListener注:如果要使用request or session scope bean,那么在web.xml里需要加上下面这段设置:org.springframework.web.context.request.RequestContextListener否则就会出现org.springframework.beans.factory.BeanCreationException: Scope request is not active for the current thread这个时候启动一下tomcat,会报错,此时还缺少spring的配置文件:applicationContext.xml创建applicationContext.xml在WEB-INF目录下创建文件名:applicationContext.xml,内容如下,先保持空:测试重启tomcat服务,验证启动过程没有异常。新建好package在Java Resources - src下新建出如下package:l com.jsdz.action -actionl com.jsdz.dao -数据库访问l com.jsdz.exception -自定义异常l erceptor -拦截器l com.jsdz.model -模型对象l com.jsdz.service -业务接口l com.jsdz.service.impl -业务接口实现l com.jsdz.test -测试目录l com.jsdz.util -工具类l com.jsdz.xml -mybatis映射XML在applicationContext.xml中添加组件注入选项这里配置成spring中注解+自动扫描方式,相对XML文件配置简化得多。Repository:DAO层组件,Service:业务层组件,Controller:控制层组件 通过这部分设置相应几类组件都自动装载注入,但依赖于代码上添加有注解。将action的创建托管给spring容器修改LoginAction.java代码,添加注解,使之能够被spring自动创建。package com.jsdz.action;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import com.opensymphony.xwork2.ActionSupport;Scope(request)Controller(loginAction)public class LoginAction extends ActionSupport private String username ;private String password ; Overridepublic 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;public void setUsername(String username) this.username = username;public String getPassword() return password;public void setPassword(String password) this.password = password;修改struts.xml配置文件,使action的创建托管给spring。 /main.jsp/login.jsp 上面的class取值保持与Controller(loginAction) 括号中的值一致,默认是类名首字母小写。到此可以做两种测试:1) 去掉LoginAction.java中的注解,验证spring是否能创建对象实例;2) 修改LoginAction.java中注解的命名,验证login.jsp是否能访问到对应的action验证业务组件的自动注入1. 创建业务组件接口以及两个实现类:LoginService、LoginServiceImpl,LoginServiceImpl2在action中不再自己负责逻辑判断,提交给LoginService来处理业务。这里创建两个实现类主要是验证当有多个实现类时如何选择注入。LoginService.javapackage com.jsdz.service;import Scope(singleton)Service(loginService)public interface LoginService boolean IsLogin(String userName,String passWord ) ;由于这个业务类中不包含任何成员变量,属于无状态类,因此可以定义创建为单实例。LoginServiceImpl.javapackage com.jsdz.service.impl;import Scope(singleton)Service(loginServiceImpl)public class LoginServiceImpl implements LoginService Overridepublic boolean IsLogin(String userName, String passWord) System.out.println(LoginServiceImpl IsLogin invoked!);if(userName.equals(admin) & passWord.equals(1234)return true;return false;LoginServiceImpl2.javapackage com.jsdz.service.impl;import Scope(singleton)Service(loginServiceImpl2)public class LoginServiceImpl2 implements LoginService Overridepublic boolean IsLogin(String userName, String passWord) System.out.println(LoginServiceImpl2 IsLogin invoked!);if(userName.equals(admin) & passWord.equals(admin)return true;return false;修改LoginAction.javapackage com.jsdz.action;.Scope(request)Controller(loginAction)public class LoginAction extends ActionSupport private String username ;private String password ; AutowiredQualifier(loginServiceImpl2)private LoginService loginService ;Overridepublic String execute() throws Exception System.out.println(LoginAction execute invoked!);/if(username.equals(admin) & password.equals(1234) )if(loginService.IsLogin(username, password)System.out.println(user ok);return SUCCESS;else System.out.println(user invalid);return INPUT ;public LoginService getLoginService() return loginService;public void setLoginService(LoginService loginService) this.loginService = loginService; .Autowired:自动注入Qualifier:多个相同类型时选择注入哪一个。6. 整合MyBatis复制MyBatis的JAR包从mybatis-3.1.1-bundle.zip包中复制mybatis-3.1.1.jar,加lib目录下6个JAR文件到该工程的WEB-INF/lib目录下。(其中commo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 六一活动糖葫芦活动方案
- 六一活动西餐厅活动方案
- 六一灯光活动策划方案
- 六一玩转蔬果活动方案
- 六一衣服活动方案
- 六反六查三确保活动方案
- 药品食品考试试题及答案
- 药品考试试题及答案解析
- 药二考试试题及答案分析
- 兰坪税务局活动方案
- 2025年普通高等学校招生全国统一考试数学试题(全国二卷)(有解析)
- 消防考试基础试题及答案
- 部编人教版一年级下册道德与法治复习计划
- 新基建浪潮下临沂市智慧交通管理的创新与突破
- 临时用电施工方案技术交底
- 厂房维修合同协议书模板
- 儿童意外异物吞食课件
- 2025年Z世代消费行为与品牌社群营销研究报告
- 富民银行笔试题库及答案
- 2025年高考第二次模拟考试数学(新高考Ⅱ卷)(参考答案)
- 2025年春季《中华民族共同体概论》第二次平时作业-国开(XJ)-参考资料
评论
0/150
提交评论