S2SH 整合 主要關注struct2

因爲要去新公司,新公司叫我要了解S2SH知識,於是花了一個上午的時間來研究S2SH,分享一下我架設S2SH流程。

工具:Myeclipse8.5

strcut2+spring2.5+hibernate3.2

最後使用工具需要的包

可能有些包是用不到了,我沒有一個一個去嘗試。還是有點依賴工具。

dao層和biz層代碼就不貼了,都是使用面向接口的變成。以前也熟練使用ssh。

下面貼上action代碼和配置文件。

這個是action類。

package com.exam.action;

import com.exam.biz.UserBiz;
import com.opensymphony.xwork2.ActionSupport;



public class UserAction extends ActionSupport {
	
	private String username;
	private String password;
	private UserBiz userBiz;
	
	
	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;
	}	

	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public String execute(){
		if ("admin".equals(this.username)&&"admin".equals(this.password)) {
			return "success"; //這裏的返回值指跟struct.xml中配置對應確定返回給那個頁面
		}else{
			return "error";
		}		
	}
	
}

通過該類大家也可以看到actionForm也在改action中。改action繼承actionsupport,並重寫execute方法,該方法有唯一的返回值String。

Struct.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>
	<!-- Rose India Struts 2 Tutorials -->
	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<constant name="struts.devMode" value="true" />
	<!--導入struts-default.xml文件 -->
	<include file="struts-default.xml"></include>

	<!-- 配置中文支持 -->
	<constant name="struts.i18n.encoding" value="utf-8"></constant>

	<package name="strut" extends="struts-default" namespace="/">

		<action name="UserAction" class="userAction">  <!--這裏的action的 class應經在spring中配置了,這裏直接調用就好,struct1.X版本有很大的區別-->
			<result name="success">/success.jsp</result>  <!--這裏的name的值就是action中execute中的返回值-->
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>    


下面是spring的配置,改spring的配置中集成了hibernate的配置

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/exam"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>

	<bean id="exampleHibernateProperties"
		class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="properties">
			<props>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.query.substitutions">true 'T', false 'F'</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.c3p0.minPoolSize">5</prop>
				<prop key="hibernate.c3p0.maxPoolSize">20</prop>
				<prop key="hibernate.c3p0.timeout">600</prop>
				<prop key="hibernate.c3p0.max_statement">50</prop>
				<prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
			</props>
		</property>
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<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>
			</props>
		</property>
	</bean>
	
	<!--dao層 -->
	<bean id="userDao" class="com.exam.dao.impl.UserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- biz -->
	<bean id="userBiz" class="com.exam.biz.impl.UserBizImpl">
		<property name="userDao" ref="userDao"></property>
	</bean>
	
	<!-- action -->  <!--這裏定義的action的class struct1中定義action不能使用id,必須要使用name屬性-->
	<bean id="userAction" class="com.exam.action.UserAction" scope="prototype">
		<property name="userBiz" ref="userBiz"></property>
	</bean>
</beans>

還有一部比較的關鍵在web.xml中配置關於struct2和spring的啓動

<?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">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	 <!-- Struts2 配置 -->
	<filter>
		<filter-name>struts</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 配置spring的監聽器 -->   
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:applicationContext.xml</param-value>  
    </context-param>  
    <!-- 開啓監聽    --> 
    <listener>   
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
    </listener>  
	
</web-app>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>     
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">     
<html>     
<head>     
         
</head>     
       
<body>     
    <center>     
        <s:form action="login" method="POST" namespace="/">     
            <s:textfield name="username" label="用戶名" size="14"/>     
            <s:password name="password" label="密 碼" size="14"/>     
            <s:submit label="提交" />     
        </s:form>     
    </center>     
</body>     
</html>   
  



配置了這個才能將strut2和spring配置結合起來使用!

struct1需要在struct配置中配置讀取spring文件。現在感覺方便很多了!但是在寫的過程中也出現過問題


問題一、

 Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: 

解決方法:我也不知道對不對,但是這樣改過之後就不報這個錯誤了。我在spring配置中的action bean上面加了一個scope="prototype"就不報這個錯誤了

錯誤2、
Unable to load configuration. - [unknown location]
解決方法:少了一個Struts-spring-plugin-2.1.8包


錯誤3、The Struts dispatcher cannot be found.  This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request 


has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
at org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:60)
at org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44)
解決方法:
1.引入
<%@ taglib prefix="s"  uri="/struts-tags" %>
2.在web.xml中未將攔截的*.action改成/* 
一般第二種 情況比較多。是沒有改WEB.XML中的攔截


錯誤4、Caused by: java.lang.IllegalStateException: The action name cannot be the same as the action suffix [Action]

解決方法:action類中的包導入錯誤:

正確的應該導入:import com.opensymphony.xwork2.ActionSupport;

而我導入的是:import org.springframework.web.struts.ActionSupport;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章