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;


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值