SSH整合的感想以及步骤(附完整工程)

经过n次错误之后,第一个简单的ssh整合的登陆程序终于完成。经验就是核心jar包一定要找对。还有从最基本的struts先开始配置。一开始配置的时候老是报一个错误,就是action找不到,最后找了好久才发现在action配置的时候extends="struts-default"写成了 extends="struts-dafault".这样的错误真是会逼疯你的节奏,还好我最后改出来了。

还有一个错误也是改了好久:在配置hbm.xml的文件的时候,文件头要写成

<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 这个错误也是弄了好久。

一般我们在引入第三方jar包的时候,喜欢在工程下新建一个libs文件夹,将jar包放进去。这个时候会存在一个问题,在你编译的时候没有错误,但是在你启动tomcat的时候,总会发现缺少各种jar包。这是因为你没有配置tomcat的classpath。tomcat找不到这些jar包。所以建议直接把这些需要的jar包复制到web-inf下的lib文件夹里。

action 应该继承ActionSupport

总的来说,整合的步骤我认为应该是这样的,

1:首先应该写struts.xml,我的struts.xml是在src根目录下的。在配置的时候也遇到过其他的问题,在网上查的时候说struts.xml要放在web-inf下。其实这没有必要,因为我用的是myeclipse开发工具,在编译的时候会自动把struts.xml复制到web-inf 目录下。

<?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.i18n.encoding" value="UTF-8"/>
	<constant name="struts.devMode" value="true"/>
	<package name="login"  extends="struts-default">
		<action name="login" class="loginAction">
			<result name="success">welcome.jsp</result>
			<result name="error">error.jsp</result>
		</action>
	</package>
</struts>

 对这个配置文件的几点说明。package name 没有很大的关系。result 中的jsp因为和index.jsp在同一级目录下,所以直接这样写就行。在index.jsp中请求此action的方式是<form action="login.action" method="post">

 如果在action中配置了namespace=“/***” ,那么请求的时候就应该是<form action="***/login.action" method="post"> 注意namespace中的/不要少。

2:配置applicationContext.xml 在这里面要配置datasource.和一些bean 使用的是spring的ioc(依赖注入)

<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver"/>
		<property name="jdbcUrl" value="jdbc:mysql://localhost/test"/>
		<property name="user" value="root"/>
		<property name="password" value="root"/>
		<property name="maxPoolSize" value="40"/>
		<property name="minPoolSize" value="1"/>
		<property name="initialPoolSize" value="1"/>
		<property name="maxIdleTime" value="20"/>	
	</bean>
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="mappingResources">
			<list>
				<value>bean/Login.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
				hibernate.hbm2ddl.auto=update
			</value>
		</property>
	</bean>
	<bean id="loginService" class="serviceImpl.LoginServiceImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="loginAction" class="action.LoginAction" scope="prototype">
		<property name="loginService" ref="loginService"></property>
	</bean>
	
</beans>

 

3:在web.xml中还要配置spring的启动。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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_3_0.xsd">
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<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>

 4:在***.hbm.xml中如下

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="bean">
	<class name="Login" table="login">
		<id name="id">
			<generator class="identity"/>
		</id>
		<property name="username" type="string"/>
		<property name="password" type="string"/>
	</class>
</hibernate-mapping>

 以上配置文件差不多就是配置ssh需要的所有。慢慢试,总会配好的。

完整工程http://download.csdn.net/detail/beiweideqidaozhe/8150859(内含所需所有jar包)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值