一.整合步驟
1. 通過(guò)MyEclipse向?qū)В砑觭truts功能
2. 通過(guò)MyEclipse向?qū)?,添加Hibernate3功能:生成會(huì)話(huà)工廠的那個(gè)步驟中一定要將那個(gè)對(duì)號(hào)要去掉,不能由hibernate來(lái)生成,而是交給Spring來(lái)生成;還有就是導(dǎo)入jar包的時(shí)候選擇復(fù)制到lib目錄下這一項(xiàng)。
3. 通過(guò)MyEclipse向?qū)?,?dǎo)入實(shí)現(xiàn)Spring功能,注意導(dǎo)入jar包的時(shí)候選擇復(fù)制到lib目錄下這一項(xiàng)。
3. 利用MyEclipse反向工程的方法,以Spring<dao>生成dao對(duì)象的方式創(chuàng)建Hibernate DAO,相關(guān)POJO及其xxx.hbm.xml。
4. DAO實(shí)現(xiàn)類(lèi)加入@Transactional標(biāo)記。
5. 修改applicationContext.xml文件,增加Spring事務(wù)管理、DAO等bean的配置。
6. 編寫(xiě)action類(lèi)。
7. 在applicationContext.xml文件中添加Action的代理bean。
8. 在struts的配置文件中,添加相應(yīng)的Action,類(lèi)名指向Spring中的代理bean,并加入<controller
processorClass="org.springframework.web.struts.DelegatingRequestProcessor"
/>和<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>。
9. 編寫(xiě)Jsp文件。
10. 發(fā)布web項(xiàng)目。
11. 啟動(dòng)web服務(wù)器,運(yùn)行項(xiàng)目
二.SSH實(shí)現(xiàn)關(guān)于書(shū)籍增刪改查實(shí)例
1.創(chuàng)建mysql數(shù)據(jù)庫(kù)及其表
create database book;
create table book(id int not null primary key auto_increment,bookname varchar(30),bookauthor varchar(30));
2.表現(xiàn)層
(1)index.jsp(首頁(yè))
- <%@ page language="java" pageEncoding="GBK" %>
- <%@ taglib uri="http://struts./tags-html" prefix="html" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html:html lang="true">
- <head>
- <html:base/><title>歡迎</title>
- </head>
- <body>
- <a href="book.do?method=listbook" mce_href="book.do?method=listbook">查看書(shū)籍列表</a><br>
- </body>
- </html:html>
(2)list.jsp(書(shū)籍列表頁(yè)面)
- <%@ page contentType="text/html;charset=GBK" isELIgnored="false"%>
- <%-- 我們使用 JSTL 來(lái)訪問(wèn)數(shù)據(jù) --%>
- <%@ taglib uri="http://java./jsp/jstl/core" prefix="c" %>
- <%
- 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>書(shū)籍列表頁(yè)面</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <mce:style><!--
-
- a:hover {
- color: red;
- text-decoration: none
- }
- --></mce:style><style mce_bogus="1">
- a:hover {
- color: red;
- text-decoration: none
- }</style>
- </head>
- <body>
- <b>書(shū)籍列表頁(yè)面</b>
- <br>
- <%-- 輸出用戶(hù)列表 --%>
- <br>
- <c:choose>
- <c:when test="${not empty books}">
- <table width="80%" border="1" cellpadding="0"
- style="border-collapse: collapse;" bordercolor="#000000">
- <tr>
- <td>
- <b>書(shū)籍ID</b>
- </td>
- <td>
- <b>書(shū)籍名稱(chēng)</b>
- </td>
- <td>
- <b>作者</b>
- </td>
- <td>
- <b>價(jià)格</b>
- </td>
- <td>
- <b>操作</b>
- </td>
- </tr>
-
- <c:forEach items="${books}" var="book">
- <tr>
- <td>
- ${book.id}
- </td>
- <td>
- ${book.bookname}
- </td>
- <td>
- ${book.bookauthor}
- </td>
- <td>
- ${book.bookprice}
- </td>
- <td>
- <a href="<%=path%>/book.do?method=modifybook&id=${book.id}">修改</a>
- <a href="<%=path%>/book.do?method=deletebook&id=${book.id}">刪除</a>
- </td>
- </tr>
- </c:forEach>
-
- </table>
- </c:when>
- <c:otherwise>抱歉,沒(méi)有找到相關(guān)的記錄!</c:otherwise>
- </c:choose>
- <a href="<%=path%>/new.jsp">添加書(shū)籍</a>
- <form action="<%=path%>/book.do?method=searchbook" method="post" onsubmit="return checkSearchForm(this);">
- <fieldset>
- <legend>
- 查找書(shū)籍
- </legend>
- 書(shū)籍名:
- <input name="bookname">
- <input type="submit" value="查找">
- </fieldset>
- </form>
- </body>
- </html>
(3)new.jsp(新增書(shū)籍頁(yè)面)
- <%@ page language="java" pageEncoding="GBK"%>
- <%@ taglib uri="http://struts./tags-bean" prefix="bean"%>
- <%@ taglib uri="http://struts./tags-html" prefix="html"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>添加書(shū)籍</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <mce:script type="text/javascript" src="js/form.js" mce_src="js/form.js"></mce:script>
- </head>
- <body>
- <h3>
- 添加書(shū)籍
- </h3>
- <form action="<%=request.getContextPath()%>/book.do?method=addbook" onsubmit="return checkForm(this);" method="post">
- <table width="100%" border="0">
- <tbody>
- <tr>
- <td>
- 書(shū)籍名:
- </td>
- <td>
-
- <input name="bookname"/>
- <br>
- </td>
- </tr>
- <tr>
- <td>
- 作者:
- </td>
- <td>
-
- <input name="bookauthor"/>
- </td>
- </tr>
- <tr>
- <td>
- 價(jià)格:
- </td>
- <td>
-
- <input name="bookprice"/>
- </td>
- </tr>
- <tr>
- <td>
-
- <input type="submit" value="添加" name="button1">
- </td>
- <td>
-
- <input type="Reset" value="重填" name="button2">
- </td>
- </tr>
- </tbody>
- </table>
- </form>
- <input type="button" onclick="document.location=''<%=request.getContextPath()%>/book.do?method=listbook'';"
- value="
- 返回列表">
- </body>
- </html>
(4)edit.jsp(書(shū)籍修改頁(yè)面)
- <%@ page language="java" pageEncoding="GBK" isELIgnored="false"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>修改書(shū)籍</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <mce:script type="text/javascript" src="js/form.js" mce_src="js/form.js"></mce:script>
- </head>
- <body>
- <h3>
- 修改書(shū)籍
- </h3>
- <form action="<%=request.getContextPath()%>/book.do?method=updatebook" onsubmit="return checkForm(this);" method="post">
- <input type="hidden" value="${book.id}" name="id"/>
- <table width="100%" border="0">
- <tbody>
- <tr>
- <td>
- 書(shū)籍名:
- </td>
- <td>
-
- <input name="bookname" value="${book.bookname}"/>
- <br>
- </td>
- </tr>
- <tr>
- <td>
- 作者:
- </td>
- <td>
-
- <input name="bookauthor" value="${book.bookauthor}"/>
- </td>
- </tr>
- <tr>
- <td>
- 價(jià)格:
- </td>
- <td>
-
- <input name="bookprice" value="${book.bookprice}"/>
- </td>
- </tr>
- <tr>
- <td>
-
- <input type="submit" value="提交" >
- </td>
- <td>
-
- <input type="reset" value="重填">
- </td>
- </tr>
- </tbody>
- </table>
- </form>
- <input type="button" onclick="document.location=''book.do?method=listbook'';" value="返回列表">
- </body>
- </html>
(5)error.jsp(錯(cuò)誤公用頁(yè)面)
- <%@ page language="java" pageEncoding="GBK" isELIgnored="false"%>
- <%
- 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>出錯(cuò)了!</title>
- </head>
- <body>
- 出錯(cuò)了!<br/>
- 詳細(xì)信息是:<br/>
- ${message}<br/><br/>
- <a href="javascript:history.back();" mce_href="javascript:history.back();">返回</a>
- </body>
- </html>
(6)form.js
-
- function checkForm(form) {
- if (form.bookname.value == "") {
- alert("書(shū)名不能為空!");
- form.bookname.focus();
- return false;
- }
- if (form.bookauthor.value == "") {
- alert("作者不能為空!");
- form.bookauthor.focus();
- return false;
- }
- if (form.bookprice.value == "") {
- alert("價(jià)格不能為空!");
- form.bookprice.focus();
- return false;
- }
- return true;
- }
- function checkSearchForm(form){
- if(form.bookname.value.match(/^/s*$/)){
- alert("查詢(xún)條件不能為空!");
- form.bookname.focus();
- return false;
- }
- return true;
- }
3.公用類(lèi)及其javabean
(1)EncodingFilter.java(過(guò)濾器)
- package filter;
- import java.io.IOException;
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- public class EncodingFilter implements Filter {
- protected FilterConfig config;
- protected String Encoding = null;
- public void init(FilterConfig config) throws ServletException {
- this.config = config;
- this.Encoding = config.getInitParameter("Encoding");
- }
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- if (request.getCharacterEncoding() == null) {
- if (Encoding != null) {
- request.setCharacterEncoding(Encoding);
- response.setCharacterEncoding(Encoding);
- }
- }
- chain.doFilter(request, response);
- }
- public void destroy() {
- }
- }
(2)book.java
- package dao;
-
-
-
- public class Book implements java.io.Serializable {
-
- private Integer id;
- private String bookname;
- private String bookauthor;
- private Float bookprice;
-
-
- public Book() {
- }
-
- public Book(String bookname, String bookauthor, Float bookprice) {
- this.bookname = bookname;
- this.bookauthor = bookauthor;
- this.bookprice = bookprice;
- }
-
- public Integer getId() {
- return this.id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getBookname() {
- return this.bookname;
- }
- public void setBookname(String bookname) {
- this.bookname = bookname;
- }
- public String getBookauthor() {
- return this.bookauthor;
- }
- public void setBookauthor(String bookauthor) {
- this.bookauthor = bookauthor;
- }
- public Float getBookprice() {
- return this.bookprice;
- }
- public void setBookprice(Float bookprice) {
- this.bookprice = bookprice;
- }
- }
4.DAO層
BookDAO.java
- package dao;
- import java.util.List;
- import org.hibernate.LockMode;
- import org.hibernate.Query;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import org.springframework.transaction.annotation.Transactional;
-
-
-
-
-
-
-
-
-
-
-
- @Transactional
- public class BookDAO extends HibernateDaoSupport {
- private static final Logger log = LoggerFactory.getLogger(BookDAO.class);
-
- public static final String BOOKNAME = "bookname";
- public static final String BOOKAUTHOR = "bookauthor";
- public static final String BOOKPRICE = "bookprice";
- protected void initDao() {
-
- }
- public void save(Book transientInstance) {
- log.debug("saving Book instance");
- try {
- getHibernateTemplate().save(transientInstance);
- log.debug("save successful");
- } catch (RuntimeException re) {
- log.error("save failed", re);
- throw re;
- }
- }
- public void update(Book transientInstance) {
- log.debug("saving Book instance");
- try {
- getHibernateTemplate().update(transientInstance);
- log.debug("save successful");
- } catch (RuntimeException re) {
- log.error("save failed", re);
- throw re;
- }
- }
- public void delete(Book persistentInstance) {
- log.debug("deleting Book instance");
- try {
- getHibernateTemplate().delete(persistentInstance);
- log.debug("delete successful");
- } catch (RuntimeException re) {
- log.error("delete failed", re);
- throw re;
- }
- }
- public Book findById(java.lang.Integer id) {
- log.debug("getting Book instance with id: " + id);
- try {
- Book instance = (Book) getHibernateTemplate().get("dao.Book", id);
- return instance;
- } catch (RuntimeException re) {
- log.error("get failed", re);
- throw re;
- }
- }
- public List findByExample(Book instance) {
- log.debug("finding Book instance by example");
- try {
- List results = getHibernateTemplate().findByExample(instance);
- log.debug("find by example successful, result size: "
- + results.size());
- return results;
- } catch (RuntimeException re) {
- log.error("find by example failed", re);
- throw re;
- }
- }
- public List findByProperty(String propertyName, Object value) {
- log.debug("finding Book instance with property: " + propertyName
- + ", value: " + value);
- try {
- String queryString = "from Book as model where model."
- + propertyName + "like = ";
- return getHibernateTemplate().find(queryString, value);
- } catch (RuntimeException re) {
- log.error("find by property name failed", re);
- throw re;
- }
- }
- public List findByBookname(String bookname) {
- String sql="from Book where bookname like ''%"+bookname+"%''";
- Query query=this.getSession().createQuery(sql);
- return query.list();
- }
- public List findByBookauthor(Object bookauthor) {
- return findByProperty(BOOKAUTHOR, bookauthor);
- }
- public List findByBookprice(Object bookprice) {
- return findByProperty(BOOKPRICE, bookprice);
- }
- public List findAll() {
- log.debug("finding all Book instances");
- try {
- String queryString = "from Book";
- return getHibernateTemplate().find(queryString);
- } catch (RuntimeException re) {
- log.error("find all failed", re);
- throw re;
- }
- }
- public Book merge(Book detachedInstance) {
- log.debug("merging Book instance");
- try {
- Book result = (Book) getHibernateTemplate().merge(detachedInstance);
- log.debug("merge successful");
- return result;
- } catch (RuntimeException re) {
- log.error("merge failed", re);
- throw re;
- }
- }
- public void attachDirty(Book instance) {
- log.debug("attaching dirty Book instance");
- try {
- getHibernateTemplate().saveOrUpdate(instance);
- log.debug("attach successful");
- } catch (RuntimeException re) {
- log.error("attach failed", re);
- throw re;
- }
- }
- public void attachClean(Book instance) {
- log.debug("attaching clean Book instance");
- try {
- getHibernateTemplate().lock(instance, LockMode.NONE);
- log.debug("attach successful");
- } catch (RuntimeException re) {
- log.error("attach failed", re);
- throw re;
- }
- }
- public static BookDAO getFromApplicationContext(ApplicationContext ctx) {
- return (BookDAO) ctx.getBean("BookDAO");
- }
- public static void main(String[] args) {
- ApplicationContext ctx =
- new
- ClassPathXmlApplicationContext("applicationContext.xml");
- BookDAO dao = (BookDAO)ctx.getBean("BookDAO");
- Book book = new Book();
- book.setBookname("數(shù)學(xué)");
- book.setBookauthor("張三");
- book.setBookprice(12.0f);
- dao.save(book);
- }
- }
5.service層
(1)IBookManager.java(接口)
- package service;
- import java.util.List;
- import dao.Book;
- public interface IBookManager {
-
-
-
-
-
-
-
- public Book findById(int id);
-
-
-
-
-
-
-
- public boolean update(Book Book);
- public boolean save(Book Book);
-
-
-
-
-
-
-
- public boolean delete(Book Book);
-
-
-
-
-
-
-
- public List<Book> findByBookname(String username);
- public List findAll();
- }
(2)BookManager.java(實(shí)現(xiàn)類(lèi))
- package service;
- import java.util.List;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import dao.Book;
- import dao.BookDAO;
- public class BookManager implements IBookManager {
- private BookDAO bookdao;
-
- public boolean delete(Book book) {
- try {
- bookdao.delete(book);
- return true;
- } catch (Exception e) {
- }
- return false;
- }
- public Book findById(int id) {
- return bookdao.findById(id);
- }
- public List findAll(){
- return bookdao.findAll();
- }
- public List<Book> findByBookname(String bookname) {
- return bookdao.findByBookname(bookname);
- }
- public boolean update(Book Book) {
- try {
- bookdao.update(Book);
- return true;
- } catch (Exception e) {
- }
- return false;
- }
- public boolean save(Book Book){
- try {
- bookdao.save(Book);
- return true;
- } catch (Exception e) {
- }
- return false;
- }
- public dao.BookDAO getBookdao() {
- return bookdao;
- }
- public void setBookdao(dao.BookDAO bookdao) {
- this.bookdao = bookdao;
- }
- }
6.Action處理
(1)BookForm.java
- package com.zxc.struts.form;
- import org.apache.struts.action.ActionForm;
- public class BookForm extends ActionForm{
- private int id;
- private String bookname;
- private String bookauthor;
- private float bookprice;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getBookname() {
- return bookname;
- }
- public void setBookname(String bookname) {
- this.bookname = bookname;
- }
- public String getBookauthor() {
- return bookauthor;
- }
- public void setBookauthor(String bookauthor) {
- this.bookauthor = bookauthor;
- }
- public float getBookprice() {
- return bookprice;
- }
- public void setBookprice(float bookprice) {
- this.bookprice = bookprice;
- }
- }
(2)BookAction.java
-
-
-
-
- package com.zxc.struts.action;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.actions.DispatchAction;
- import service.IBookManager;
- import com.zxc.struts.form.BookForm;
- import dao.Book;
-
-
-
-
-
-
-
- public class BookAction extends DispatchAction {
- private IBookManager bookManager;
- public ActionForward addbook(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
-
- BookForm bookForm=(BookForm)form;
- Book book=new Book();
- book.setBookname(bookForm.getBookname());
- book.setBookauthor(bookForm.getBookauthor());
- book.setBookprice(bookForm.getBookprice());
- bookManager.save(book);
- return listbook(mapping,form,request,response);
- }
- public ActionForward updatebook(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
-
- BookForm bookForm=(BookForm)form;
- String id=request.getParameter("id");
- Book book=bookManager.findById(Integer.parseInt(id));
- book.setBookname(bookForm.getBookname());
- book.setBookauthor(bookForm.getBookauthor());
- book.setBookprice(bookForm.getBookprice());
- if(bookManager.update(book)){
- return listbook(mapping,form,request,response);
- }else{
- String message="更新失敗!";
- request.setAttribute("message", message);
- return mapping.findForward("message");
- }
- }
- public ActionForward modifybook(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
-
- String id=request.getParameter("id");
- Book book=bookManager.findById(Integer.parseInt(id));
- request.setAttribute("book", book);
- return mapping.findForward("edit");
- }
- public ActionForward deletebook(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
-
- String id=request.getParameter("id");
- Book book=bookManager.findById(Integer.parseInt(id));
- if(bookManager.delete(book)){
- return listbook(mapping,form,request,response);
- }else{
- String message="刪除失敗!";
- request.setAttribute("message", message);
- return mapping.findForward("message");
- }
- }
- public ActionForward listbook(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
-
- List books=bookManager.findAll();
- request.setAttribute("books", books);
- return mapping.findForward("list");
- }
- public ActionForward searchbook(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
-
- String bookname=request.getParameter("bookname");
- List books=bookManager.findByBookname(bookname);
- request.setAttribute("books", books);
- return mapping.findForward("list");
- }
- public void setBookManager(IBookManager bookManager) {
- this.bookManager = bookManager;
- }
- public IBookManager getBookManager() {
- return bookManager;
- }
- }
7.配置文件
(1)log4j.properties
- log4j.rootLogger=WARN, stdout
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
(2)hibernate.cfg.xml
- <?xml version=''1.0'' encoding=''UTF-8''?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate./hibernate-configuration-3.0.dtd">
- <!-- Generated by MyEclipse Hibernate Tools. -->
- <hibernate-configuration>
- <session-factory>
- <property name="dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <property name="connection.url">
- jdbc:mysql:
- </property>
- <property name="connection.username">root</property>
- <property name="connection.password">123</property>
- <property name="connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
- <property name="myeclipse.connection.profile">mysql5</property>
- <mapping resource="dao/Book.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
(3)book.hbm.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate./hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="dao.Book" table="book" catalog="book">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="increment" />
- </id>
- <property name="bookname" type="java.lang.String">
- <column name="bookname" length="30" />
- </property>
- <property name="bookauthor" type="java.lang.String">
- <column name="bookauthor" length="30" />
- </property>
- <property name="bookprice" type="java.lang.Float">
- <column name="bookprice" precision="12" scale="0" />
- </property>
- </class>
- </hibernate-mapping>
(4)struts-config.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts./dtds/struts-config_1_3.dtd">
- <struts-config>
- <form-beans>
- <form-bean name="bookForm" type="com.zxc.struts.form.BookForm"/>
- </form-beans>
- <global-exceptions />
- <global-forwards />
- <action-mappings >
- <action
- path="/book"
- name="bookForm"
- parameter="method"
- type="com.zxc.struts.action.BookAction"
- cancellable="true" >
- <forward name="list" path="/list.jsp"/>
- <forward name="edit" path="/edit.jsp"/>
- <forward name="message" path="/error.jsp"/>
- </action>
- </action-mappings>
- <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
- <message-resources parameter="com.zxc.struts.ApplicationResources" />
- <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
- <set-property property="contextConfigLocation"
- value="/WEB-INF/classes/applicationContext.xml" />
- </plug-in>
- </struts-config>
(5)applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www./schema/beans"
- xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:p="http://www./schema/p"
- xmlns:tx="http://www./schema/tx"
- xsi:schemaLocation="http:
- http:
- http:
- http:
- ">
- <tx:annotation-driven transaction-manager="transactionManager"
- proxy-target-class="true" />
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation" value="classpath:hibernate.cfg.xml">
- </property>
- </bean>
- <bean id="BookDAO" class="dao.BookDAO">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
- <!-- 聲明一個(gè) Hibernate 3 的事務(wù)管理器供代理類(lèi)自動(dòng)管理事務(wù)用 -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref local="sessionFactory" />
- </property>
- </bean>
- <!-- book業(yè)務(wù)處理類(lèi)-->
- <bean id="bookManager" class="service.BookManager">
- <property name="bookdao">
- <ref local="BookDAO"/>
- </property>
- </bean>
- <bean name="/book" class="com.zxc.struts.action.BookAction">
- <property name="bookManager">
- <ref local="bookManager"/>
- </property>
- </bean>
- </beans>
(6)web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://java./xml/ns/javaee" xmlns:xsi="http://www./2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java./xml/ns/javaee http://java./xml/ns/javaee/web-app_2_5.xsd">
- <servlet>
- <servlet-name>action</servlet-name>
- <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>/WEB-INF/struts-config.xml</param-value>
- </init-param>
- <init-param>
- <param-name>debug</param-name>
- <param-value>3</param-value>
- </init-param>
- <init-param>
- <param-name>detail</param-name>
- <param-value>3</param-value>
- </init-param>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <!--過(guò)濾器 -->
- <filter>
- <filter-name>Filter</filter-name>
- <filter-class>
- filter.EncodingFilter<!-- 過(guò)濾器類(lèi) -->
- </filter-class>
- <init-param>
- <param-name>Encoding</param-name>
- <param-value>gbk</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>Filter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>