最近因为面试要弄框架,把我弄疯了。好吧,固然编码业务我还可以,但是框架只熟悉SpringMVC+MyBatis还是不行的。故脱了某个SSH的项目拿出来溜溜,加深下记忆,同时帮助下一些朋友及初学者们。。。如果编码者看到这些配置文件,别惊慌,没错 小弟我就是脱了你的。。。
框架会搭会用即可。。
首先创建一个Web工程,这个相信人人都会。然后就没有然后了。。。好了 会了么?不会 接着看下去。。。
WEB-INF导入对应jar包,具体哪些包自己去找,我就不说了。
然后先不管那么多,创建struts.xml文件-然而创建这个文件并没有多大的用,只是说明下即可
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- 开启使用开发模式,详细错误提示 --> <constant name="struts.devMode" value="false" /> <!-- 将对象交给spring管理 --> <constant name="struts.objectFactory" value="spring" /> <!-- 指定资源编码类型 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 指定每次请求到达,重新加载资源文件 --> <constant name="struts.i18n.reload" value="false" /> <!-- 指定每次配置文件更改后,自动重新加载 --> <constant name="struts.configuration.xml.reload" value="false" /> </struts>
别问我web.xml怎么改,反正只要会搭就好了 顺序我不管了。。
然后创建spring.xml,这个其实也就一些说明(jdbc路径啦,使用注解啦,配置文件路径啦)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 使用 注解配置--> <context:annotation-config /> <!-- 使用 注解自动注册 --> <context:component-scan base-package="com.ssh" /> <!-- 数据库配置 这一段我就不说啥了 实在是基本jdbc都配在这 我用的是Mysql--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/sbf"> </property> <property name="username" value="root"></property> <property name="password" value=""></property> </bean> <!-- sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> <!-- 可以自动创建数据库表(create),不创建(none) --> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 包扫描的方式加载注解类(推荐) --> <property name="packagesToScan"> <list> <value>com.ssh.*</value> </list> </property> </bean> <!--JDBC事务管理器,根据你的情况使用不同的事务管理器,如果工程中有Hibernate,就用Hibernate的事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <!-- 用注解来实现事务管理 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
再然后我们就可以开开心心的改编web.xml了。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 加载spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring*.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- struts2 的配置 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> <init-param> <!-- //固定格式--> <param-name>actionPackages</param-name> <param-value>com.ssh</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
web.xml也弄好了,我们就可以开开心心写代码了。别问我代码怎么写,注解写法就那样写。如果要项目的,欢迎加QQ群248148860
我是伤寒,把这个项目丢到群共享了。进来自己翻开下把
MySql数据库是sbf,表只创建一个提供你测试的user表,字段是 int类型的userid varchar 长度50的userName 和 varchar 长度50的passWord