Spring 框架做一个项目步骤
SSH框架图
如:整个登录程序行走流程
首先导入基本的包,一个有46个,如下,下载地址
页面大小原因部分加载的包显示不出来,全部加载即可
1. 创建各代码类实体domain,包括.java 和.hbm.xml
2. 创建spring配置
3.配置web.xml
3.1 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 1.1 spring配置文件位置
* 方式1: 【建议】
<param-value>classpath:spring/applicationContext.xml</param-value>
需要在applicationContext.xml 配置<import> 导入其他
* 方式2:
<param-value>classpath:spring/applicationContext*.xml</param-value>
加载所有,不需要配置<import>
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!-- 1.2 加载spring配置文件使用监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
4.配置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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 公共配置项 -->
<!-- 1.1 加载properties文件 -->
<context:property-placeholder location="classpath:jdbcInfo.properties"/>
<!-- 1.2配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc:driverClass}"></property>
<property name="jdbcUrl" value="${jdbc:jdbcUrl}"></property>
<property name="user" value="${jdbc:user}"></property>
<property name="password" value="${jdbc:password}"></property>
</bean>
<!-- 2.配置 SessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<!-- 配置c3p0 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingLocations" value="classpath:com/itheima/crm/*/domain/*.hbm.xml"></property>
</bean>
<!-- 3 事务管理 -->
<!-- 3.1 事务管理器-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 3.2 事务详情
*增删改:读写
*查询:只读;
-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="login" read-only="true"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 3.3 AOP编程
*强制cglib(只有接口): <aop:config proxy-target-class="true">
-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.crm.*.service..*.*(..))"/>
</aop:config>
<!-- 导入其他配置文件 -->
<import resource="applicationContext-staff.xml"/>
</beans>
5.配置 XML Catalog,增加struts-2.3.dtd
key 填写 -//Apache Software Foundation//DTD Struts Configuration 2.3//EN
6.配置struts.xml
6.1 struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 1 常量 -->
<!-- 1.1开发模式 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 1.2标签主题,简单风格 -->
<constant name="struts.ui" value="true"></constant>
<!-- 2公共项 -->
<package name="common" namespace="/" extends="struts-default"></package>
<!-- 3加载其他配置文件 -->
<include file="struts/struts-staff.xml"></include>
</struts>
6.2 struts-staff.xml 等其他配置文件
struts-staff.xm
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 员工配置 -->
<package name="sta" namespace="/" extends="common">
<action name="staffAction_*" class="com.itheima.crm.staff.web.action.staffAction" method="{1}">
<!-- 1 登录成功 -->
<result name="success" type="redirectAction">staffAction_home</result>
<!-- 2 登录失败 -->
</action>
</package>
</struts>
7. 配置web.xml文件里面的struts前端控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 1.1 spring配置文件位置
* 方式1: 【建议】
<param-value>classpath:spring/applicationContext.xml</param-value>
需要在applicationContext.xml 配置<import> 导入其他
* 方式2:
<param-value>classpath:spring/applicationContext*.xml</param-value>
加载所有,不需要配置<import>
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!-- 1.2 加载spring配置文件使用监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 2 struts 前端控制器 -->
<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>
</web-app>
8. 写独立部分类的Dao和Service类并在对应的applicationContext-*.xml里面注入
其中Dao层需要注入sessionFactory
public class StaffDaoImpl extends HibernateDaoSupport implements StaffDao{
//HibernateDaoSupport需要sessionFactory里面的c3p0...连接数据源,所以有sessionFactory,之后在spring配置dao层时,需要注入SessionFactory
@Override
public CrmStaff find(String loginName, String loginPwd) {
List<CrmStaff> allStaff = this.getHibernateTemplate().find("from crm_staff where loginName = ? and loginPwd = ?", new Object[]{loginName,loginPwd});
if(allStaff.size()==1){
return allStaff.get(0);
}
return null;
}
}
applicationContext-staff.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 员工配置项 -->
<bean id="staffDao" class="com.itheima.crm.staff.dao.impl.StaffDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="staffService" class="com.itheima.crm.staff.service.impl.StaffServiceImpl">
<property name="staffDao" ref="staffDao"></property>
</bean>
</beans>
service类省略
9.写action,相当于servlet,但是不做判断,交由struts.xml判断。注入service,继承ActionSupport,实现接口ModelDriven<T>
package com.itheima.crm.staff.web.action;
import com.itheima.crm.staff.domain.CrmStaff;
import com.itheima.crm.staff.service.StaffService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.org.apache.bcel.internal.generic.RETURN;
public class staffAction extends ActionSupport implements ModelDriven<CrmStaff> {
//封装数据
/*
* modelDriven接口可以在action中直接获得例如User对象,它会将Object getModel()方法取得的User放到ValueStack中。可以理解为将这个User的属性追加到Action中。它主要是作用是实现类似Struts的FormBean功能。
getmodel()方法返回一个用于接收用户输入数据的对象模型,在这个模型对象中的属性可以直接通过(属性名)userName来访问,而不需要使用(对象名.属性名)user.userName这种格式来访问了,
在action也不需要对对象提供getter和setter方法了,但是必须要在action中进行new操作。
*/
private CrmStaff staff = new CrmStaff();
@Override
public CrmStaff getModel() {
return staff;
}
//默认按照名称注入
private StaffService staffService;
public void setStaffService(StaffService staffService) {
this.staffService = staffService;
}
//
/**
* 员工登录
*/
public String login(){
CrmStaff findstaff = staffService.login(staff);
if(findstaff!=null){
//成功
//session作用域保存数据
ActionContext.getContext().getSession().put("loginStaff", findstaff);
return "success";
}
//TODO 不成功
return "";
}
}
交由struts.xml判断转接,相当于controller,纯做判断
struts-staff.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 员工配置 -->
<package name="sta" namespace="/" extends="common">
<action name="staffAction_*" class="com.itheima.crm.staff.web.action.staffAction" method="{1}">
<!-- 1 登录成功 -->
<result name="success" type="redirectAction">staffAction_home</result>
<!-- 2 登录失败 -->
</action>
</package>
</struts>