Eclipse中 struts2 + spring3 + mybatis3 环境搭建

公司要用struts2 + spring3 + mybatis3做项目,尝试着在家鼓捣了一下。

1. 框架下载

mybatis-3.2.2.zip 和mybatis-spring-1.2.0-bundle.zip

2. 建示例工程

在Eclipse中新建示例工程,步骤就不详述了,我把工程取名为ssm_example。

3. struts2配置

3.1src下创建struts.xml配置文件

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"""><struts><constant name="struts.devMode" value="true" /><package name="basic" extends="struts-default"><action name="index" class="cn.ssm.sample.action.IndexAction" method="execute"><result name="success">/WEB-INF/jsp/Index.jsp</result></action></package></struts>3.2修改web.xml<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>3.3从解压的struts-2.3.14-all的lib文件夹下,挑选了必须的和比较常用的jar包,放入WEB-INF/lib的文件夹下commons-fileupload-1.2.2.jarcommons-io-2.0.1.jarcommons-lang-2.4.jarcommons-lang3-3.1.jarcommons-logging-1.1.1.jarcommons-logging-api-1.1.jarfreemarker-2.3.19.jarjavassist-3.11.0.GA.jarognl-3.0.6.jarstruts2-core-2.3.14.jarxwork-core-2.3.14.jar3.4建个测试用的IndexAction.java和Index.jsppackage cn.ssm.sample.action;import com.opensymphony.xwork2.ActionSupport;public class IndexAction extends ActionSupport{@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubreturn super.execute();}}<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>Hello world!</body></html>3.5测试struts2

运行服务,在浏览器输入:8080/ssm_example/index,如果配置没有错误,那么会出现Hello world!

4. spring3配置

4.1 config/spring下创建applicationContext.xml配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi="" xmlns:aop=""xmlns:tx="" xmlns:jdbc=""xmlns:context=""xsi:schemaLocation=" http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!– enable component scanning (beware that this does not enable mapper scanning!) –><context:component-scanbase-package="cn.ssm.sample.action,cn.ssm.sample.service" /><!– enable autowire –><context:annotation-config /><!– enable transaction demarcation with annotations –><tx:annotation-driven /></beans>4.2修改web.xml<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/applicationContext.xml</param-value></context-param>4.3 添加相关jar包aopalliance-1.0.jarspring-aop-3.2.2.RELEASE.jarspring-beans-3.2.2.RELEASE.jarspring-context-3.2.2.RELEASE.jarspring-core-3.2.2.RELEASE.jarspring-expression-3.2.2.RELEASE.jarspring-jdbc-3.2.2.RELEASE.jarspring-tx-3.2.2.RELEASE.jarspring-web-3.2.2.RELEASE.jarstruts2-spring-plugin-2.3.14.jar4.4 修改struts.xml文件

添加:<constant name="struts.objectFactory" value="spring"></constant>

并且把class="cn.ssm.sample.action.IndexAction"改成class="indexAction"

4.5 修改Action

在IndexAction的类上,,加上@Controller,这样Spring就能自动实例化成indexAction对象了。

4.6 测试spring

运行服务,在浏览器输入:8080/ssm_example/index,如果配置没有错误,那么依然会出现Hello world!

5. mybatis3配置

5.1 数据库准备

因为在路上你就已经收获了自由自在的好心情。

Eclipse中 struts2 + spring3 + mybatis3 环境搭建

相关文章:

你感兴趣的文章:

标签云: