SSH集成相关配置

时间:2022-05-14 14:23:16

SSH项目整合

1. 加上lib

2. struts.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>

 

</struts>

3. 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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

 

 

<bean id="dataSource"

class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName"

value="com.microsoft.sqlserver.jdbc.SQLServerDriver">

</property>

<property name="url"

value="jdbc:sqlserver://localhost:1433;database=SSH">

</property>

<property name="username" value="sa"></property>

<property name="password" value="123456"></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.SQLServerDialect

</prop>

<prop key="hibernate.hbm2ddl.auto">

update

</prop>

</props>

</property>

</bean>

</beans>

4. web.xml

<context-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>classpath:applicationContext.xml</param-value>

 </context-param>

 <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>

 <listener>

  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

 </listener>

 

 

------------------------------------------------

5. 登录页面的form表单

    <form action="loginAction" method="post">

    用户名:<input type="text" name="user.name" size="20" /><br/>

    用户名:<input type="password" name="user.pwd" size="20" /><br/>

    <input type="submit" value="提交" size="20" /><br/>

</form>

6. action类的创建

 

public class UserAction extends ActionSupport{

//User属性,而且该对象的namePwd属性都能够被自动赋值

private User user;

 

public User getUser() {

return user;

}

 

public void setUser(User user) {

this.user = user;

}

public String executeLogin(){

if(("ysj").equals(user.getName())){

return SUCCESS;//常量

}else{

return INPUT;

}

}

 

}

 

 

7. struts.xml配置文件,配置action

 

<!-- 配置action -->

<package name="pk" extends="struts-default">

<!-- 与配置servlet一样 -->

<action name="loginAction" class="action.UserAction" method="executeLogin">

<result name="success">/wel.jsp</result>

<result name="input">/fail.jsp</result>

</action>

</package>

8. domain中的类

 

public class User implements Serializable{

private String id;

private String name;

private String pwd;

 

9. User.hbm.xml映射文件的配置

 

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC 

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="domain">

<class name="User" table="user_table">

<id name="id">

<generator class="UUID">

</generator>

</id>

<property name="name" column="name"></property>

<property name="pwd" column="pwd"></property>

</class>

</hibernate-mapping>

 

10. 将映射文件加入sessionFactory的配置中

 

<property name="mappingResources">

<list>

<value>domain/User.hbm.xml</value>

</list>

</property>

11. dao层代码的搭建

 

public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

 

public int getUser(User user) {

String sql = "from User where name=? and pwd=?";

return this.getHibernateTemplate().

find(sql,user.getName(),user.getPwd()).size();

}

 

}

12. applicationContext.xml中配置daobean

 

<bean id="userDao" class="dao.impl.UserDaoImpl">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

 

13. 测试springHibernate的结合

 

public class Test {

//执行完该Main方法后,数据库中是否生成了一张user-table

public static void main(String[] args) {

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

UserDao dao = (UserDao) ac.getBean("userDao");

User user = new User();

dao.getUser(user);

}

 

}

14. service层的而实现

a) private UserDao userDao

15. 配置service,做dao的注入

<bean id=userService class=””>

<proterty name= userDao ref=userDao />

</bean>

16. 修改action

private UserService userService

 

 

选作:登录成功后,显示用户列表

listAction   UserService