Struts2+hibernate+Spring框架整合

目录

创建spring项目,不选包

整合Hibernate,不选包

整合strut2

创建实体类和action类

创建action类编写测试代码,如果能获得前端传来的值,就算成功了

strut2配置action

创建一个新的spring  bean给action赋值:

测试

配置文件


首先

创建spring项目,不选包

整合Hibernate,不选包

整合strut2

创建实体类和action类

创建action类编写测试代码,如果能获得前端传来的值,就算成功了

strut2配置action

创建一个新的spring  bean给action赋值:

他赋值的原理是在web.xml中有由一个struts2的过滤器,当我们设置的action的class为不存在的class,就继续往下走,然后就会到spring的监听器,然后查询对应class名字的bean name 就会接管注值的责任

测试

配置文件

struts.xml

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="zk" extends="struts-default">
		<action name="loginAction" class="loginAction" method="login">
			<result>index.jsp</result>
		</action>
		

	</package>

</struts>

LoginAction.java

package com.bdqn.action;

import com.bdqn.entitydao.Users1DAO;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	private String name;
	private String pass;
	private Users1DAO users1dao;
	
	
	public String login() throws Exception {
		System.out.println("guole");
		System.out.println(name);
		System.out.println(users1dao.findAll());
		return SUCCESS;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public Users1DAO getUsers1dao() {
		return users1dao;
	}
	public void setUsers1dao(Users1DAO users1dao) {
		this.users1dao = users1dao;
	}
	
	
	
	
}

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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="oracle.jdbc.OracleDriver">
		</property>
		<property name="url"
			value="jdbc:oracle:thin:@localhost:1521:orcl1">
		</property>
		<property name="username" value="system"></property>
		<property name="password" value="1234"></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.Oracle9Dialect
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/bdqn/entitydao/Goods.hbm.xml</value>
				<value>com/bdqn/entitydao/Users1.hbm.xml</value></list>
		</property></bean>
	<bean id="GoodsDAO" class="com.bdqn.entitydao.GoodsDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="Users1DAO" class="com.bdqn.entitydao.Users1DAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean name="loginAction" class="com.bdqn.action.LoginAction"
		abstract="false" lazy-init="default" autowire="default"
		p:users1dao-ref="Users1DAO">
	</bean></beans>

web.xml

<?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">
  <display-name></display-name>	
  	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
  </body>
</html>

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'login.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>
    <form action="/newSSH2/loginAction.action">
    	name:<input type="text" name="name"><br>
    	pass:<input type="password" name="pass"><br>
    	<input type="submit" name="tijiao"><br>
    </form>
  </body>
</html>

猜你喜欢

转载自blog.csdn.net/jinqianwang/article/details/82188311