Struts2+Hibernate+Spring框架搭建
一:搭建Struts2的框架
1.首先新建一个"WebProject",名称为----FirstSshProject
2.将Struts2所需要的Jar包拷贝到“WEB-INF”下面的 lib目录中
Struts2所需要的Jar包如下
3.在src目录下新建包“com.wl.struts.action”,在此包下新建一个名为“loginAction”的类,并且继承“ActionSupport”。重写这个类的“execute()”方法
loginAction.java的代码如下:
package com.wl.struts.action;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class loginAction extends ActionSupport {
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
package com.wl.struts.action;
importcom.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class loginActionextends ActionSupport {
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
4.
1)修改WebRoot下面的“index.jsp”文件
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<a href="login.action">test action</a>
</body>
</html>
<%@ pagelanguage="java" import="java.util.*"pageEncoding="GB18030"%>
<%
String path =request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>My JSP 'index.jsp' startingpage</title>
<meta http-equiv="pragma"content="no-cache">
<meta http-equiv="cache-control"content="no-cache">
<meta http-equiv="expires"content="0">
<meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This ismy page">
<!--
<link rel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<a href="login.action">testaction</a>
</body>
</html>
2)在WebRoot目录下面添加success.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
Success. <br>
</body>
</html>
<%@ pagelanguage="java" import="java.util.*"pageEncoding="GB18030"%>
<%
String path =request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>My JSP 'success.jsp' startingpage</title>
<meta http-equiv="pragma"content="no-cache">
<meta http-equiv="cache-control"content="no-cache">
<meta http-equiv="expires"content="0">
<meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This ismy page">
<!--
<link rel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
Success. <br>
</body>
</html>
3)在WebRoot目录下面添加error.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'error.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
Error. <br>
</body>
</html>
<%@ pagelanguage="java" import="java.util.*"pageEncoding="GB18030"%>
<%
String path =request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>My JSP 'error.jsp' startingpage</title>
<meta http-equiv="pragma"content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires"content="0">
<meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This ismy page">
<!--
<link rel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
Error. <br>
</body>
</html>
5.在src目录下面新建一个struts.xml的文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="login" namespace="/" extends="struts-default">
<action name="login" class="com.wl.struts.action.loginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
<?xmlversion="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTDStruts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="login"namespace="/" extends="struts-default">
<action name="login"class="com.wl.struts.action.loginAction">
<resultname="success">/success.jsp</result>
<resultname="error">/error.jsp</result>
</action>
</package>
</struts>
6.修改“Web.xml”的配置信息,将Struts2的配置信息添加进去
<?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>
<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>
<?xmlversion="1.0" encoding="UTF-8"?>
<web-appversion="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>
<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>
7.使用Tomcat部署项目,启动Tomcat,输入"http://localhost:8080/FirstSshProject_001"
点击“test action ”连接之后,结果如下:
经过以上操作步骤,Strut2的环境搭建完毕。
二:创建数据库和搭建Hibernate环境配置
1.首先将Hibernate的Jar包拷贝到“WEB-INF”的lib目录下面
2.使用Oracle数据库创建表Student
3.在src目录下新建包“com.wl.po”,在这个包下面“Student”的类及对应的映射文件“student.hbm.xml”
Student.java
package com.wl.po;
public class Student {
private String id;
private String stuName;
private String stuNo;
private String stuSex;
private int stuAge;
private String stuGrade;
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuGrade() {
return stuGrade;
}
public void setStuGrade(String stuGrade) {
this.stuGrade = stuGrade;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
package com.wl.po;
public class Student {
private String id;
private String stuName;
private String stuNo;
private String stuSex;
private int stuAge;
private String stuGrade;
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuGrade() {
return stuGrade;
}
public void setStuGrade(String stuGrade) {
this.stuGrade = stuGrade;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
student.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="com.wl.po">
<class name="Student" table="student">
<id name="id">
<generator class="assigned"></generator>
</id>
<property name="stuName" column="stuName" type="java.lang.String"></property>
<property name="stuNo" column="stuNo" type="java.lang.String"></property>
<property name="stuSex" column="stuSex" type="java.lang.String"></property>
<property name="stuGrade" column="stuGrade" type="java.lang.String"></property>
<property name="stuAge" column="stuAge" type="java.lang.Integer"></property>
</class>
</hibernate-mapping>
<?xmlversion="1.0"?>
<!DOCTYPE hibernate-mappingPUBLIC
"-//Hibernate/Hibernate MappingDTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mappingpackage="com.wl.po">
<class name="Student"table="student">
<id name="id">
<generatorclass="assigned"></generator>
</id>
<property name="stuName"column="stuName"type="java.lang.String"></property>
<property name="stuNo"column="stuNo" type="java.lang.String"></property>
<property name="stuSex"column="stuSex"type="java.lang.String"></property>
<property name="stuGrade"column="stuGrade"type="java.lang.String"></property>
<property name="stuAge"column="stuAge"type="java.lang.Integer"></property>
</class>
</hibernate-mapping>
4.在src下面新建包“com.wl.dao”,在下面新建接口“studentDao"
package com.wl.dao;
import com.wl.po.Student;
public interface studentDao {
public void save(Student stu);
public void delete(Student stu);
public void update(Student stu);
}
package com.wl.dao;
import com.wl.po.Student;
public interface studentDao {
public void save(Student stu);
public void delete(Student stu);
public void update(Student stu);
}
5.在src下面新建包“com.wl.dao.impl”,在下面新建类“studentDaoImpl"
package com.wl.dao.impl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.wl.dao.studentDao;
import com.wl.po.Student;
public class studentDaoImpl extends HibernateDaoSupport implements studentDao{
public void save(Student stu){
getHibernateTemplate().save(stu);
}
public void delete(Student stu){
getHibernateTemplate().delete(stu);
}
public void update(Student stu){
getHibernateTemplate().update(stu);
}
}
package com.wl.dao.impl;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.wl.dao.studentDao;
import com.wl.po.Student;
public class studentDaoImplextends HibernateDaoSupport implements studentDao{
public void save(Student stu){
getHibernateTemplate().save(stu);
}
public void delete(Student stu){
getHibernateTemplate().delete(stu);
}
public void update(Student stu){
getHibernateTemplate().update(stu);
}
}
6.在src下新建包”com.wl.service“,在下面新建接口"studentService"
package com.wl.service;
import com.wl.po.Student;
public interface studentService {
public void saveStudent(Student stu);
public void updateStudent(Student stu);
public void deleteStudent(Student stu);
}
package com.wl.service;
import com.wl.po.Student;
public interface studentService{
public void saveStudent(Student stu);
public void updateStudent(Student stu);
public void deleteStudent(Student stu);
}
7.在src下新建包“com.wl.serviceImpl”,在下面新建类“serviceImpl”
package com.wl.service.impl;
import com.wl.dao.studentDao;
import com.wl.po.Student;
import com.wl.service.studentService;
public class studentServiceImpl implements studentService {
private studentDao stuDao;
public studentDao getStuDao() {
return stuDao;
}
public void setStuDao(studentDao stuDao) {
this.stuDao = stuDao;
}
public void deleteStudent(Student stu) {
// TODO Auto-generated method stub
stuDao.delete(stu);
}
public void saveStudent(Student stu) {
// TODO Auto-generated method stub
stuDao.save(stu);
}
public void updateStudent(Student stu) {
// TODO Auto-generated method stub
stuDao.update(stu);
}
}
package com.wl.service.impl;
import com.wl.dao.studentDao;
import com.wl.po.Student;
importcom.wl.service.studentService;
public class studentServiceImplimplements studentService {
private studentDao stuDao;
public studentDao getStuDao() {
return stuDao;
}
public void setStuDao(studentDao stuDao) {
this.stuDao = stuDao;
}
public void deleteStudent(Student stu) {
// TODO Auto-generated method stub
stuDao.delete(stu);
}
public void saveStudent(Student stu) {
// TODO Auto-generated method stub
stuDao.save(stu);
}
public void updateStudent(Student stu) {
// TODO Auto-generated method stub
stuDao.update(stu);
}
}
8.配置Hibernate的映射文件Hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- properties -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>
<property name="connection.username">WANGLEI</property>
<property name="connection.password">leiwang</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<!-- mapping files -->
<mapping resource="com/wl/po/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
三:搭建Spring
1.添加Spring的Jar包
2.添加Struts2,Spring插件的Jar包
3.添加Oracle10g 数据库的Jar包
4.使用Spring来管理struts2的Action,DAO层,Service层
1)在原来web.xml基础之上添加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>
<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>
<listener>
<!--监听器在default情况下读取的是:/WEB-INF/applicationContext.xml -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!--
<param-value>
/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
</param-value>
-->
<!-- 监听器启动后会读取classpath目录下面的beans.xml文件 -->
<param-value>classpath:beans.xml</param-value>
</context-param>
</web-app>
<?xmlversion="1.0" encoding="UTF-8"?>
<web-appversion="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>
<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>
<listener>
<!--监听器在default情况下读取的是:/WEB-INF/applicationContext.xml -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!--
<param-value>
/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
</param-value>
-->
<!-- 监听器启动后会读取classpath目录下面的beans.xml文件 -->
<param-value>classpath:beans.xml</param-value>
</context-param>
</web-app>
2)修改loginAction的内容
package com.wl.struts.action;
import com.opensymphony.xwork2.ActionSupport;
import com.wl.po.Student;
import com.wl.service.studentService;
@SuppressWarnings("serial")
public class loginAction extends ActionSupport {
private studentService stuService;
public studentService getStuService() {
return stuService;
}
public void setStuService(studentService stuService) {
this.stuService = stuService;
}
@Override
public String execute() throws Exception {
Student stu=new Student();
stu.setId("10");
stu.setStuName("Zhangsan");
stu.setStuAge(29);
stu.setStuGrade("A");
stu.setStuNo("201017");
stu.setStuSex("male");
stuService.saveStudent(stu);
return SUCCESS;
}
}
package com.wl.struts.action;
importcom.opensymphony.xwork2.ActionSupport;
import com.wl.po.Student;
importcom.wl.service.studentService;
@SuppressWarnings("serial")
public class loginActionextends ActionSupport {
private studentService stuService;
public studentService getStuService() {
return stuService;
}
public void setStuService(studentService stuService) {
this.stuService = stuService;
}
@Override
public String execute() throws Exception {
Student stu=new Student();
stu.setId("10");
stu.setStuName("Zhangsan");
stu.setStuAge(29);
stu.setStuGrade("A");
stu.setStuNo("201017");
stu.setStuSex("male");
stuService.saveStudent(stu);
return SUCCESS;
}
}
3)在Src目录下新建bean.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:contentx="http://www.springframework.org/schema/content"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/content
http://www.springframework.org/schema/content/spring-content-2.5.xsd">
<!-- configure the sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- manager the dao -->
<bean id="studentDao" class="com.wl.dao.impl.studentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- manager the service -->
<bean id="studentService" class="com.wl.service.impl.studentServiceImpl">
<property name="stuDao" ref="studentDao"></property>
</bean>
<!-- manager the action in struts2 -->
<bean id="loginAction" class="com.wl.struts.action.loginAction" scope="prototype">
<property name="stuService">
<ref bean="studentService"/>
</property>
</bean>
</beans>
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:contentx="http://www.springframework.org/schema/content"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/content
http://www.springframework.org/schema/content/spring-content-2.5.xsd">
<!-- configure the sessionFactory -->
<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="hibernateTemplate"class="org.springframework.orm.hibernate3.HibernateTemplate">
<propertyname="sessionFactory"ref="sessionFactory"></property>
</bean>
<!-- manager the dao -->
<bean id="studentDao"class="com.wl.dao.impl.studentDaoImpl">
<propertyname="sessionFactory"ref="sessionFactory"></property>
</bean>
<!-- manager the service -->
<bean id="studentService"class="com.wl.service.impl.studentServiceImpl">
<property name="stuDao"ref="studentDao"></property>
</bean>
<!-- manager the action in struts2 -->
<bean id="loginAction"class="com.wl.struts.action.loginAction"scope="prototype">
<property name="stuService">
<refbean="studentService"/>
</property>
</bean>
</beans>
4)这时部署项目到Tomcat,运行Tomcat,结果如下
点击“test Action”操作,报500错
说明未将studentService注入到loginAction中。
A:这个问题的原因是:loginAction没有真正的纳入Spring的管理当中
Spring的bean.xml配置文件的确对loginAction做了配置
<!-- manager the action in struts2 -->
<bean id="loginAction" class="com.wl.struts.action.loginAction" scope="prototype">
<property name="stuService">
<ref bean="studentService"/>
</property>
</bean>
<!-- manager the action in struts2 -->
<bean id="loginAction"class="com.wl.struts.action.loginAction"scope="prototype">
<propertyname="stuService">
<refbean="studentService"/>
</property>
</bean>
但是Struts.xml配置文件中
<package name="login" namespace="/" extends="struts-default">
<action name="login" class="com.wl.struts.action.loginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
<packagename="login" namespace="/"extends="struts-default">
<action name="login"class="com.wl.struts.action.loginAction">
<resultname="success">/success.jsp</result>
<resultname="error">/error.jsp</result>
</action>
</package>
没有用到Spring管理的loginAction,而是用类名重新引用的。loginAction
B:解决这个问题的方法:
修改Struts.xml的配置信息:
- <
package name="login" namespace="/" extends="struts-default"> <action name="login" class="loginAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> <packagename="login" namespace="/"extends="struts-default"> <action name="login"class="loginAction"> <resultname="success">/success.jsp</result> <resultname="error">/error.jsp</result> </action> </package>
将原来的class="com.wl.struts.action.loginAction"替换为class="loginAction",其中的loginAction就是Spring中定义的id为loginAction的bean。
备注:
Spring对Struts的Action管理配置中不能忘记scope="prototype"
默认情况下,Spring从bean工厂所取得的实例为Singleton(bean的singleton属性) Singleton: Spring容器只存在一个共享的bean实例,默认的配置。 Prototype: 每次对bean的请求都会创建一个新的bean实例;二者选择的原则:有状态的bean都使用Prototype作用域,而对无状态的bean则应该使用singleton作用域。
在ssh2 项目中,struts2的action交由spring管理的时候,spring默认是singleton的,而struts2的action显然是有状态的,所以必须显示设置为scope="prototype",prototype为原型模式,每次action请求过来都会创建一个action但是对那些Dao的实现类推介scope="singleton" ,因为这些类没有状态,用singleton只需维护一个实例,显然性能高一些。
5)重新部署项目,启动Tomcat后,点击“test Action”连接后控制条结果如下:
但是从数据库中查找数据,数据没有添加成功。
A: 问题原因:缺少对事务的管理,数据未持久化到数据库中
B:解决的方法:添加对事务的管理,在bean.xml文件中添加配置信息
<!-- Transaction configuration Information -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
<!-- 执行com.wl.service包及其子包下面的任何类的以Student结尾的任何方法 -->
<aop:config>
<aop:advisor pointcut="execution(* com.wl.service..*.*Student(..))" advice-ref="txAdvice" />
</aop:config>
<!-- Transaction configuration Information-->
<bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"propagation="REQUIRED" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<!-- 执行com.wl.service包及其子包下面的任何类的以Student结尾的任何方法 -->
<aop:config>
<aop:advisorpointcut="execution(* com.wl.service..*.*Student(..))"advice-ref="txAdvice" />
</aop:config>
现在重新启动Tomcat后,点击“test Action”连接后,就会将数据持久化到数据库中。