SSH整合
环境:
jdk7,Tomcat8
Myeclipse,mysql
创建Web项目
添加Spring开发功能
右键项目,Myeclipse,Project Facet,选择Spring
勾选“Spring Persistence”jar包,如下图所示。
添加Hibernate开发功能
右键项目,Myeclipse,Project Facet,选择Habernate
取消勾选“Create SessionFactory class?”,如下图所示
在applicationContext.xml文件中dataSource的bean中设置加载驱动
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property><!--添加此处--> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="1234"></property> </bean>
新建model包,反向生成POJO类,并注意改POJO类中的照片的格式为byte[]类型,以及POJO类的映射文件中,照片字段的“type=”java.lang.String””删除掉;
反向生成需要Myeclipse先连接数据库
右上角增加Database Explorer
右键数据库new
反向生成javabean
选择Hibernate,需要注意的几点:
- 勾选前两个对勾
- 选择主键生成策略:assigned为默认,native为自增
新建dao包,编写添加学生信息的DAO;
- 这里需要编写一个获得session的类BaseDao,配置bean
- 编写impl实现类,配置bean
public class UserDaoImpl extends BaseDao implements UserDao{//继承BaseDao @Override public void save(Xsb xs) { Session session = super.getSession();//BaseDao工具类获取session Transaction ts = session.beginTransaction();//开启事务 session.save(xs);//保存操作 ts.commit();//提交事务 session.close();//关闭session } @Override public Xsb findOne(Integer xh) { Session session = super.getSession(); Transaction ts = session.beginTransaction(); Query query = session.createQuery("from Xsb where xh = ?");//查询 query.setParameter(0, xh);//设置?号 List<Xsb> list= query.list();//遍历 Xsb user = list.get(0);//查询一个时,可以直接获得第一个 ts.commit(); session.close(); if(user!=null) return user; else return null; } @Override public List<Xsb> findAll() { Session session = super.getSession(); Transaction ts = session.beginTransaction(); Query query = session.createQuery("from Xsb"); List<Xsb> list = query.list(); ts.commit(); session.close(); if(list!=null) return list; else return null; } }
<bean id="baseDao" class="com.jk.dao.BaseDao" >
<property name="sessionFactory" ref="sessionFactory"></property><!--set方法注入-->
</bean>
<bean id="userDao" class="com.jk.dao.impl.UserDaoImpl" parent="baseDao"></bean><!--指定父类-->
spring的set方法注入:
- 在类中声明
- 生成get/set方法
- 配置文件中配置
property
新建service包,编写添加学生信息的service
编写impl实现类,配置bean
- 注入dao
<bean id="userService" class="com.jk.service.impl.UserServiceImpl"> <property name="userdao" ref="userDao"></property> </bean>
添加Struts2开发功能
加入jsp页面
添加jar包;
创建struts.xml:src目录下
整合细节:action的class属性值为applicationContext.xml文件中注入的action的id
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="test" extends="struts-default"> <action name="addXsView" class="userAction" method="addXsView"> <result name="success">/addXsInfo.jsp</result> </action> </package> </struts>
解释:
package:
- name:必须,随意取
- extends:必须,用于识别这些struts标签
action:
- name:请求链接
- class:applicationContext.xml文件中注入的action类的bean的id
- method:指定执行哪个方法,不指定就默认执行excute方法
result:
- name:action方法返回的字符串
- 值:地址
在web.xml中添加过滤器;
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>com.mycompany.myapp.actions</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
创建struts.properties文件(写的代码为struts.objectFactory=spring)
ssh整合的关键,表示struts.xml使用spring来加载Bean
新建action包,创建action
public class userAction extends ActionSupport{//action需要继承ActionSupport private UserService userService;//set方法注入 public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } private Xsb xs;//获取前端表单传来的值 public Xsb getXs() { return xs; } public void setXs(Xsb xs) { this.xs = xs; } @Override public String execute() throws Exception { Integer xh1=xs.getXh(); if((userService.findOne(xh1))!=null){ return ERROR; } Xsb xs1=new Xsb();//先创建一个对象 xs1.setXh(xs.getXh());//将获取的值传入新对象中 xs1.setXm(xs.getXm()); xs1.setXb(xs.getXb()); xs1.setCssj(xs.getCssj()); xs1.setBz(xs.getBz()); userService.save(xs1);//保存新对象 return SUCCESS;//SUCCESS是struts内部定义的字符串,使用时大写,不加双引号,也可以自己返回字符串,如"ok" } public String addXsView(){ return SUCCESS; } public String xsInfo(){ HttpServletRequest request = ServletActionContext.getRequest();//获得request对象 List<Xsb> list = userService.findAll(); request.setAttribute("list", list);//将值保存到request中 return SUCCESS; }
struts获取前端的值:
类中声明对象,不用创建
生成get/set方法
修改表单对应的数据
<s:form action="addXs" method="post" enctype="multipart/form-data"><!--使用s:form--> <table border="0" cellspacing="0" cellpadding="1"> <tr> <td> <s:textfield name="xs.xh" label="学号" value=""></s:textfield><!--xs为类中声明的变量名,xh为对象的属性--> </td> </tr>
这时提交表单,数据就会保存在对象中。但是保存操作时,因为对象没有实例化,所以不能直接保存。需要先new一个新的对象,用set和get方法将数据保存进新对象中
获取request对象的方法:
HttpServletRequest request = ServletActionContext.getRequest();
<s:iterator value="#request.list" id="xs"><!--遍历获取值--> <tr> <td><s:property value="#xs.xh"/></td><!--显示值--> <td><s:property value="#xs.xm"/></td> <td> <s:if test="#xs.xb==1">男</s:if> <s:else>女</s:else> </td> <td><s:property value="#xs.cssj"/></td> <td><s:property value="#xs.zxf"/></td> <td> <a href="findXs .action?xs.xh=<s:property value="#xs.xh"/>">详细信息</a> </td> </tr> </s:iterator>
遇到的坑:
404或者提交表单出现空指针异常
原因:baseDao的bean没有配置完成,检查applicationContext.xml
最终的配置文件
application.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/jk/model/User.hbm.xml</value>
<value>com/jk/model/Xsb.hbm.xml</value></list>
</property></bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!--以下为手动配置的-->
<bean id="baseDao" class="com.jk.dao.BaseDao" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userDao" class="com.jk.dao.impl.UserDaoImpl" parent="baseDao">
</bean>
<bean id="userService" class="com.jk.service.impl.UserServiceImpl">
<property name="userdao" ref="userDao"></property>
</bean>
<bean id="userAction" class="com.jk.action.userAction">
<property name="userService" ref="userService"></property>
</bean>
</beans>
struts.xml
<?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>
<package name="test" extends="struts-default">
<action name="addXsView" class="userAction" method="addXsView">
<result name="success">/addXsInfo.jsp</result>
</action>
<action name="addXs" class="userAction" >
<result name="success">/success.jsp</result>
<result name="error">/existXs.jsp</result>
</action>
<action name="xsInfo" class="userAction" method="xsInfo">
<result name="success">/xsInfo.jsp</result>
</action>
</package>
</struts>