ssi+freemarker整合

本文详细介绍了使用Spring、Struts2和MyBatis(SSM)框架整合搭建一个Web应用的过程,包括Maven项目的配置、web.xml和struts.xml的设置、Spring的bean管理以及数据库操作的具体实现。
摘要由CSDN通过智能技术生成

现在好像企业也比较流行ssi吧,那就整一个玩玩。

先上pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.web</groupId>
  <artifactId>my-web</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>my-web Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-core</artifactId>
    	<version>3.1.2.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-aop</artifactId>
    	<version>3.1.2.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-aspects</artifactId>
    	<version>3.1.2.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-jdbc</artifactId>
    	<version>3.1.2.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-orm</artifactId>
    	<version>3.1.2.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-web</artifactId>
    	<version>3.1.2.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.struts</groupId>
    	<artifactId>struts2-spring-plugin</artifactId>
    	<version>2.3.4.1</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.struts</groupId>
    	<artifactId>struts2-core</artifactId>
    	<version>2.3.4.1</version>
    	<exclusions>
    		<exclusion>
    			<artifactId>tools</artifactId>
    			<groupId>com.sun</groupId>
    		</exclusion>
    	</exclusions>
    </dependency>
    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>5.1.21</version>
    </dependency>
    <dependency>
    	<groupId>org.aspectj</groupId>
    	<artifactId>aspectjweaver</artifactId>
    	<version>1.7.1</version>
    </dependency>
    <dependency>
    	<groupId>commons-dbcp</groupId>
    	<artifactId>commons-dbcp</artifactId>
    	<version>1.4</version>
    </dependency>
    <dependency>
    	<groupId>jsptags</groupId>
    	<artifactId>pager-taglib</artifactId>
    	<version>2.0</version>
    </dependency>
    <dependency>
    	<groupId>log4j</groupId>
    	<artifactId>log4j</artifactId>
    	<version>1.2.15</version>
    	<exclusions>
    		<exclusion>
    			<artifactId>jmxri</artifactId>
    			<groupId>com.sun.jmx</groupId>
    		</exclusion>
    		<exclusion>
    			<artifactId>jmxtools</artifactId>
    			<groupId>com.sun.jdmk</groupId>
    		</exclusion>
    		<exclusion>
    			<artifactId>jms</artifactId>
    			<groupId>javax.jms</groupId>
    		</exclusion>
    		<exclusion>
    			<artifactId>mail</artifactId>
    			<groupId>javax.mail</groupId>
    		</exclusion>
    	</exclusions>
    </dependency>
    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-simple</artifactId>
    	<version>1.5.10</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.ibatis</groupId>
    	<artifactId>ibatis-sqlmap</artifactId>
    	<version>2.3.4.726</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.taglibs</groupId>
    	<artifactId>taglibs-standard</artifactId>
    	<version>1.2.1</version>
    	<type>zip</type>
    	<classifier>source-release</classifier>
    </dependency>
    	<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.20</version>
		</dependency>
  </dependencies>
  <build>
    <finalName>my-web</finalName>
  </build>
</project>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  id="WebApp_ID" version="2.5">
<display-name>my-web</display-name>
  
  <!-- Reads request input using UTF-8 encoding -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:applicationContext.xml,
        classpath*:applicationContext-*.xml
     </param-value>
  </context-param>
  <filter>
    <filter-name>struts-cleanup</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts-cleanup</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>

   <servlet> 
  <servlet-name>JspSupportServlet</servlet-name>
  <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
  <load-on-startup>1</load-on-startup> 
 </servlet>
  
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
     <constant name="struts.ognl.allowStaticMethodAccess" value="true" />
  <!-- 禁用调试模式 -->
    <constant name="struts.devMode" value="false" />
    <!-- 禁用动态方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <!-- 指定struts过滤的扩展名,多个用逗号分隔 -->
  <constant name="struts.action.extension" value="action" />
    <!-- 指定struts上传文件路径 -->
    <!-- <constant name="struts.multipart.saveDir" value="D:/music/temp" /> -->
    <!-- 指定struts 上传时的文件最大Size 20M-->
  <constant name="struts.multipart.maxSize" value="20480000" />
  <!--解决乱码    -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- 指定struts通过spring生成Action实例 -->
    <constant name="struts.objectFactory" value="spring" />
  
    <package name="default" extends="struts-default">
        <default-action-ref name="index" />

        <global-results>
        	<result name="relogin" type="redirect">/</result>
            <result name="error">/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>
    
        <action name="index">
            <result>/index.jsp</result>
        </action>
    </package>

    <include file="struts-action.xml"/>

</struts>

struts-action.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
  <package name="login" extends="struts-default">
        <action name="login" class="loginAction" method="login">
            <result name="success" type="freemarker">/WEB-INF/login/helloworld.ftl</result>
        </action>
    </package>
</struts>

applicationContext.xml:

<?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:aop ="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation ="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
    <!-- spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean -->
    <context:component-scan base-package="com.jesse" />
    
    <!-- 获取JDBC配置文件 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <value>classpath:jdbc.properties</value>
    </property>
  </bean>
  <!-- MySql数据源的配置,采用DBCP连接池 -->
  <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
  </bean>
  
  <!-- SqlMap setup for iBATIS Database Layer -->
  <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation" value="classpath:SqlMapConfig.xml" />
    <property name="dataSource" ref="dataSource" />
  </bean>
  
  <!-- 事务 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
    	<property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- AOP定义pointcut -->
    <aop:config>
    	<aop:pointcut id="commonTx" expression="execution(public * com.jesse.service.impl.*.*(..))"/>
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="commonTx"/>
    </aop:config>
    
    <!-- 通过aop实现hibernate事务及事务传递    -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
    	<tx:attributes>
      <tx:method name="add*"/>
      <tx:method name="update*"/>
      <tx:method name="del*"/>
      <tx:method name="save*"/>
      <tx:method name="edit*"/>
      <tx:method name="modify*"/>
      <tx:method name="*" read-only="true"/>
    	</tx:attributes>
    </tx:advice>
  
</beans>

SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>   
<!DOCTYPE sqlMapConfig    
    PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"    
    "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
    
  <settings cacheModelsEnabled="false" enhancementEnabled="false"
    lazyLoadingEnabled="false" maxRequests="3000" maxSessions="3000"
    maxTransactions="3000" useStatementNamespaces="false" />
  
  <sqlMap resource="com/jesse/model/User.xml" />
  
</sqlMapConfig>

User.xml:

<?xml version="1.0" encoding="UTF-8"?>    
<!DOCTYPE sqlMap    
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"    
"http://www.ibatis.com/dtd/sql-map-2.dtd">   
 <sqlMap namespace="User">       
 	<typeAlias alias="user" type="com.jesse.model.User" />   
    <resultMap  id="userResult" class="user">   
		<result property="id" column="ID" />   
        <result property="name" column="NAME" />   
        <result property="password" column="PASSWORD" />   
    </resultMap>      
  	<select id="User.queryUserByName" resultClass="user" parameterClass="string">
	    SELECT * FROM gg_user where NAME=#condition#
  	</select>
</sqlMap>

基本配置就这么多,其实框架用起来麻烦的地方就是搭建的时候了,各种配置好繁琐,一下是demo源码:

http://pan.baidu.com/s/1bnhbKkv






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值