首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

SSH 1.3调整例子

2012-11-04 
SSH 1.3整合例子首选创建web.xml 主要是配置Struts的ActionServlet和Spring的字符过滤器1. ?xml version

SSH 1.3整合例子
首选创建web.xml 主要是配置Struts的ActionServlet和Spring的字符过滤器

   1. <?xml version="1.0" encoding="UTF-8"?> 
   2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
   3.    
   4.   <filter> 
   5.     <filter-name>CharacterEncodingFilter</filter-name> 
   6.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
   7.     <init-param> 
   8.       <param-name>encoding</param-name> 
   9.       <param-value>UTF-8</param-value> 
  10.     </init-param> 
  11.     <init-param> 
  12.       <param-name>forceEncoding</param-name> 
  13.       <param-value>true</param-value> 
  14.     </init-param> 
  15.   </filter> 
  16.    
  17.   <filter-mapping> 
  18.     <filter-name>CharacterEncodingFilter</filter-name> 
  19.     <url-pattern>/*</url-pattern> 
  20.   </filter-mapping> 
  21.    
  22.   <servlet> 
  23.     <servlet-name>action</servlet-name> 
  24.     <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 
  25.     <init-param> 
  26.       <param-name>config</param-name> 
  27.       <param-value>/WEB-INF/struts/struts.xml</param-value> 
  28.     </init-param> 
  29.     <init-param> 
  30.       <param-name>debug</param-name> 
  31.       <param-value>3</param-value> 
  32.     </init-param> 
  33.     <init-param> 
  34.       <param-name>detail</param-name> 
  35.       <param-value>3</param-value> 
  36.     </init-param> 
  37.     <load-on-startup>0</load-on-startup> 
  38.   </servlet> 
  39.    
  40.   <servlet-mapping> 
  41.     <servlet-name>action</servlet-name> 
  42.     <url-pattern>*.do</url-pattern> 
  43.   </servlet-mapping> 
  44.    
  45.   <!-- session超时定义,单位为分钟 --> 
  46.   <session-config> 
  47.     <session-timeout>10</session-timeout> 
  48.   </session-config> 
  49.    
  50.   <!-- 默认首页定义 --> 
  51.   <welcome-file-list> 
  52.     <welcome-file>index.jsp</welcome-file> 
  53.     <welcome-file>index.html</welcome-file> 
  54.   </welcome-file-list> 
  55.    
  56. </web-app> 

1===========1
然后创建struts.xml(String配置文件) 要注意这里集成了Spring插件,把全部Spring配置文件注入到ContextLoaderPlugIn中

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

  <form-beans>
<form-bean name="loginVO" type="com.vo.LoginVO" />
  </form-beans>

  <global-forwards>
   <forward name="error" path="/error.jsp"/>
  </global-forwards>

  <action-mappings>
   <action path="/login"
     name="loginVO"
      type="org.springframework.web.struts.DelegatingActionProxy"
      parameter="action"
      scope="request">
      <forward name="login" path="/login.jsp"/>
      <forward name="index" path="/index.jsp"/>
    </action>
  </action-mappings>

  <!-- 集成Spring插件 -->
  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/spring/*.xml" />
  </plug-in>

</struts-config>

===============
配置Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 读入属性文件 -->
<bean id="propertyConfig" value="${hibernate.driverClassName}" />
  <property name="url" value="${hibernate.url}" />
  <property name="username" value="${hibernate.username}" />
  <property name="password" value="${hibernate.password}" />
     <property name="maxActive" value="${hibernate.maxActive}" />
  <property name="maxIdle" value="${hibernate.maxIdle}" />
  <property name="maxWait" value="${hibernate.maxWait}" />
    </bean>

<!-- 配置Hibernate的Session工厂,注入数据源、映射文件 -->
    <bean id="sessionFactory" abstract="true" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 配置Action,singleton属性设置为false表示不使用单例,每次都重新创建实例,避免并发问题,注入事务管理的Service -->
  <bean name="/login" singleton="false">
   <property name="loginService">
         <ref bean="loginService"/>
        </property>
  </bean>
</beans>
===================
配置Service将Dao注入到Service

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 配置事务代理Service,先将Dao注入到Service,再将Service注入给事务代理 -->
    <bean id="loginService" parent="transactionProxy">
     <property name="target">
      <ref bean="loginTempService"/>
     </property>
   </bean>
   <bean id="loginTempService" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 配置Dao,注入Session工厂 -->
    <bean id="loginDao" table="login">
        <comment></comment>
       
        <id name="id" type="int">
   <column name="id" />
   <generator />
  </id>
       
        <property name="username" type="string">
            <column name="username" not-null="true">
                <comment></comment>
            </column>
        </property>
       
        <property name="password" type="string">
            <column name="password" not-null="true">
                <comment></comment>
            </column>
        </property>
    </class>
</hibernate-mapping>

==============================
jsp页面文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
  <form name="form" action="login.do?action=login" method="post">
   <h1>Login</h1>
   <h4>username:</h4><input name="username" type="text">&nbsp;&nbsp;&nbsp;<span id="message"></span>
   <h4>password:</h4><input name="password" type="password">
   <br>
   <br>
   <input value="提交" type="button" onclick="form.submit();">
  </form>
  <br>
  <br>
  <br>
  <form name="form" action="login.do?action=save" method="post">
   <h1>Save</h1>
   <h4>username:</h4><input name="username" type="text">&nbsp;&nbsp;&nbsp;<span id="message"></span>
   <h4>password:</h4><input name="password" type="password">
   <br>
   <br>
   <input value="提交" type="button" onclick="form.submit();">
  </form>
</body>
</html>

=================






热点排行