spring -structs 2整合spring

spring -structs 2整合spring
1)拷jar包
2)配置Structs2核心控制器和配置文件
先写web.xml,再写structs2.xml
3)配置web应用在启动时自动创建Spring容器
用ContextLoaderListener,实现了ContextInitialized(ServletContextEvent sce),在该方法
new xmlwebapplicationContext(“beans.xml”)
spring容许通过contextConfigLocation的参数来指定配置文件名
4)为第三步提供配置文件
5)添加structs2-spring整合的插件包

——以上为安装步骤,只需要一次即可———
开发过程:
1)Action不再主动获取service组件
2)Action为service 提供的setter方法务必于service的配置id一致(byname)

整合hibernate
spring整合hibernate的优势:
1.通用的资源管理,直接管理hibernate底层的dataSource,SessionFactory
2.有效的Session管理
3.声明式事务管理
4.异常包装spring把所有的checked异常都包装成runtime异常
安装步骤:
1)配置DataSource和SessionFactory
2)为第一步提供hibernate.cfg.xml

门面模式:
如果客户端组件要完成某个功能时,需要依次调用N个组件的方法,此时就应该考虑为N个组件封装成一个门面,当客户端组件只需要与门面交互。
hibernate查询:
1)打开session
2)打开事务
3)传入HQL创建Query
4)为HQL语句参数
5)执行查询,返回结果
6)提交事务
7)关闭session等资源
最关心:传入hql语句,返回结果

Ajax:(不建议说这个)
1)创建XHR对象
2)打开远程的链接
3)设置回调函数
4 ) 发送请求
最关心:ajax.get(url.data.cb)
ajax.post(url.data.cb)
service封装dao
aciton直接调用dao,完成一次业务逻辑,action需要依次调用N个dao的方法,所以我们会用service来封装n 个dao组件,此时serivce就相当于多个dao的门面

封装一个BaseDaoHibernate4工具类,相当于HQL查询的门面
appCtx.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:util="http://www.springframework.org/schema/util"
        xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd ">
       
<bean id="bookService" class="org.fkjava.s2sh.service.BookServiceImpl">

<!--  以下为事务配置-->
<bean id="transactionManager" class=" org.springframework.orm.hibernate5.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*" timeout="5"/>
</tx:attributes>
</tx:advice>
<aop:config>

<aop:pointcut expression="execution(*org.fkjava.s2sh.service.impl.*.*(..))" id="fkpo"/>
</aop:config>

daoCtx.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:util="http://www.springframework.org/schema/util"
        xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/beans/spring-context.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDateSource"
p:dieverClass="${driver}"
p:jdbcUrl="${url}"
p:user="${user}"
p:password="${password}"
p:maxPoolSize="${maxSize}"
p:minPoolSize="${minSize}"
p:initialPoolSize="${initSize}"/>

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
 p:configLoaction="classpath:hibernate.cfg.xml"  
 p:dateSource-ref="dataSource" />
 <bean id="bookDao"class=""
 p:sessionFactory-ref="sessionFactory">
 
 
 </bean>
</beans>

structs.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

        <constant name="struts.enable.DynamicMethodInvocation"
                  value="false" />
        <constant name="struts.devMode"
              value="true" />
  <package name="fkjvava" namespace="/" extends="struts-default">
<action name="*">
<result>/WEB-INF/content/{0}.jsp</result>

</action>
<action name="addBook" class='org.fkjava.s2sh.action.BookAction' method="add">
<result name="success">/WEB-INF/content/succcess.jsp</result>
<result name="error">/WEB-INF/content/error.jsp</result>
</action>
 </package>
</struts>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory >
	    <property name="hibernate.dialect">org.hibernate.dialect.MYSQL5InnoDBDialect</property>
		<property name="hibernate.show_sql">true</property>
			<property name="hibernate.format_sql">true</property>
				<property name="hibernate.hbm2ddl.auto">update</property>
		<mapping resource="org.fkjava.s2sh.domain.Book"/>
		
	</session-factory>
</hibernate-configuration>

book.java

package org.fkjava.s2sh.domain;

import javax.persistence.*;


@Entity
@Table(name="book_tb")
public class Book {
@Id
@Column(name="book_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="book_name")
private String name;
private double price;
private String author;

public Integer getId() {
	return id;
}
public void setId(Integer id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public double getPrice() {
	return price;
}
public void setPrice(double price) {
	this.price = price;
}
public String getAuthor() {
	return author;
}
public void setAuthor(String author) {
	this.author = author;
}

}

BookAction.java

package org.fkjava.s2sh.action;


import org.fkjava.s2sh.domain.Book;
import org.fkjava.s2sh.service.BookService;

import com.opensymphony.xwork2.ActionSupport;

public class BookAction extends ActionSupport {
/**
	 * 
	 */
private static final long serialVersionUID = 1L;
private Book book;
private BookService bookService;

public Book getBook() {
	return book;
}

public void setBook(Book book) {
	this.book = book;
}

public void setBookService(BookService bookService) {
	this.bookService = bookService;
}

public String add() {
	
	Integer id =bookService.addBook(book);
	if(id>0)
		return "SUCCESS";
	else
		return "ERROR";
}
}

BookService.java

package org.fkjava.s2sh.service;

import org.fkjava.s2sh.domain.Book;

public interface BookService {
	Integer addBook(Book book);
}

BookServiceImp.java

package org.fkjava.s2sh.service;

import org.fkjava.s2sh.domain.Book;

public class BookServiceImpl implements BookService {

	@Override
	public Integer addBook(Book book) {
		System.out.println("模拟添加图书"+book.getName());
		return 19;
	}

}

BookFrom.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%@ taglib prefix="s"  uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加图书</title>
</head>
<body>
<form method="POST"  action="addBook">
<div class="form-group">
<label for="bookName">图书名</label>
<input type="text" class="form-control" id="bookName" >

</div>

</form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <filter>
    <filter-name>structs2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>structs2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/appCtx.xml</param-value>
  </context-param>
</web-app>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值