MyBatis 3章 MyBatis Spring Struts2 整合应用

来源:互联网 发布:智能电网调度算法 编辑:程序博客网 时间:2024/06/11 05:26

1、技术目标

  • 为项目添加Struts2框架
  • 整合Spring与strtus2
  • 为项目添加jsp页面,操作影片CRUD

注意:关于strtus2框架其他方面的应用细节不在本文讨论范围

2、使用准备

2.1) 在项目(Web)中新增如下jar包,struts版本2.2.1.1(本文已提供下载):

commons-fileupload-1.2.1.jar

commons-io-1.3.2.jar

commons-logging-1.1.1.jar

freemarker-2.3.16.jar

javassist-3.12.0.GA.jar

ognl-3.0.jar

struts2-core-2.2.1.1.jar

struts2-spring-plugin-2.2.1.1.jar

xwork-core-2.2.1.1.jar

2.2)创建如下包,放置struts控制器(Action)代码:

com.xxx.web.struts.action

2.3)在src下创建Spring配置文件applicationContext-actions.xml ,内容如下:

Xml代码  收藏代码
  1. <?xml version="1.0"encoding="UTF-8"?> 
  2.     <beansxmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p" 
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  5.  
  6.     <!-- 这里配置Action --> 
  7.      
  8. </beans> 

2.4)在src下创建struts2配置文件struts.xml ,内容如下:

Xml代码  收藏代码
  1. <?xml version="1.0"encoding="UTF-8"?> 
  2.     <!DOCTYPE struts PUBLIC 
  3.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
  4.         "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5.      
  6.     <struts> 
  7.         <constantname="struts.enable.DynamicMethodInvocation"value="true"/> 
  8.         <constantname="struts.devMode"value="true"/> 
  9.         <constantname="struts.i18n.encoding"value="UTF-8"/> 
  10.         <constantname="struts.objectFactory"value="spring"/> 
  11.         <constantname="struts.objectFactory.spring.autoWire"value="type"/> 
  12.         <constantname="struts.ui.theme"value="simple"></constant> 
  13.         <packagename="film"namespace="/film"extends="struts-default"> 
  14.             <!-- 设置Action --> 
  15.         </package> 
  16. </struts> 

2.5)web.xml 中配置struts2

Xml代码  收藏代码
  1. <?xml version="1.0"encoding="UTF-8"?> 
  2.     <web-appversion="2.5"  
  3.         xmlns="http://java.sun.com/xml/ns/javaee"  
  4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  6.         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
  7.       <welcome-file-list> 
  8.         <welcome-file>index.jsp</welcome-file> 
  9.       </welcome-file-list> 
  10.        
  11.         <context-param> 
  12.             <param-name>contextConfigLocation</param-name> 
  13.             <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> 
  14.         </context-param> 
  15.          
  16.         <filter> 
  17.             <filter-name>struts2</filter-name> 
  18.             <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  19.         </filter> 
  20.         <filter-mapping> 
  21.             <filter-name>struts2</filter-name> 
  22.             <url-pattern>/*</url-pattern> 
  23.         </filter-mapping> 
  24.          
  25.         <listener> 
  26.             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  27.         </listener> 
  28.        
  29.     </web-app> 

3、在com.xxx.web.struts.action包下创建类FilmAction,如下:

Java代码  收藏代码
  1. package com.xxx.web.struts.action; 
  2.  
  3. import java.util.List; 
  4. import org.springframework.beans.factory.annotation.Autowired; 
  5. import com.opensymphony.xwork2.ActionSupport; 
  6. import com.opensymphony.xwork2.ModelDriven; 
  7. import com.xxx.pojo.Film; 
  8. import com.xxx.service.FilmService; 
  9.  
  10. @SuppressWarnings("serial"
  11. public class FilmActionextends ActionSupport implements ModelDriven<Film> { 
  12.  
  13.     //业务逻辑对象 
  14.     @Autowired 
  15.     private FilmService filmService; 
  16.      
  17.     //封装查询结果 
  18.     private List<Film> filmList; 
  19.      
  20.     //封装页面提交的表单数据 
  21.     private Film film = new Film(); 
  22.      
  23.     /**
  24.      * 获取所有的电影
  25.      * @return
  26.      * @throws Exception
  27.      */ 
  28.     public String findFilm() throws Exception { 
  29.          
  30.         this.filmList = this.filmService.getAllFilm(); 
  31.         return SUCCESS; 
  32.     } 
  33.      
  34.     /**
  35.      * 根据影片ID获取影片信息
  36.      * @return
  37.      * @throws Exception
  38.      */ 
  39.     public String detailFilm() throws Exception { 
  40.  
  41.         int id = film.getId().intValue(); 
  42.         Film film = this.filmService.getFilmById(id); 
  43.         this.film.setFname(film.getFname()); 
  44.         return SUCCESS; 
  45.     } 
  46.      
  47.     /**
  48.      * 添加影片
  49.      * @return
  50.      * @throws Exception
  51.      */ 
  52.     public String insertFilm() throws Exception { 
  53.  
  54.         this.filmService.insertFilm(film); 
  55.         return SUCCESS; 
  56.     } 
  57.      
  58.     /**
  59.      * 修改影片
  60.      * @return
  61.      * @throws Exception
  62.      */ 
  63.     public String updateFilm() throws Exception { 
  64.  
  65.         this.filmService.updateFilm(film); 
  66.         return SUCCESS; 
  67.     } 
  68.      
  69.     /**
  70.      * 删除影片
  71.      * @return
  72.      * @throws Exception
  73.      */ 
  74.     public String deleteFilm() throws Exception { 
  75.  
  76.         int id = film.getId().intValue(); 
  77.         this.filmService.deleteFilm(id); 
  78.         return SUCCESS; 
  79.     } 
  80.      
  81.     public Film getModel() { 
  82.         return film; 
  83.     } 
  84.  
  85.     public List<Film> getFilmList() { 
  86.         return filmList; 
  87.     } 
  88.  
  89.     public void setFilmList(List<Film> filmList) { 
  90.         this.filmList = filmList; 
  91.     } 
  92.  

4、在applicationContext-actions.xml中配置FilmAction

Xml代码  收藏代码
  1. <?xml version="1.0"encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p" 
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  5.  
  6.     <!-- 创建FilmAction --> 
  7.     <bean id="filmAction"     
  8.         class="com.xxx.web.struts.action.FilmAction" 
  9.         scope="prototype"/> 
  10.      
  11. </beans> 

5、在struts.xml中配置Action

Xml代码  收藏代码
  1. <?xml version="1.0"encoding="UTF-8"?> 
  2. <!DOCTYPE struts PUBLIC 
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5.  
  6. <struts> 
  7.     <constantname="struts.enable.DynamicMethodInvocation"value="true"/> 
  8.     <constantname="struts.devMode"value="true"/> 
  9.     <constantname="struts.i18n.encoding"value="UTF-8"/> 
  10.     <constantname="struts.objectFactory"value="spring"/> 
  11.     <constantname="struts.objectFactory.spring.autoWire"value="type"/> 
  12.     <constantname="struts.ui.theme"value="simple"></constant> 
  13.      
  14.     <packagename="film"namespace="/film"extends="struts-default"> 
  15.          
  16.         <!-- 获取所有影片 --> 
  17.         <actionname="findFilm"class="filmAction"method="findFilm"> 
  18.             <resultname="success">/manager/films.jsp</result> 
  19.         </action> 
  20.         <!-- 添加影片 --> 
  21.         <actionname="insertFilm"class="filmAction"method="insertFilm"> 
  22.             <resultname="success"type="redirectAction">findFilm</result> 
  23.         </action> 
  24.         <!-- 获取影片详情 --> 
  25.         <actionname="detailFilm"class="filmAction"method="detailFilm"> 
  26.             <resultname="success">/manager/updateFilm.jsp</result> 
  27.         </action> 
  28.         <!-- 修改影片 --> 
  29.         <actionname="updateFilm"class="filmAction"method="updateFilm"> 
  30.             <resultname="success"type="redirectAction">findFilm</result> 
  31.         </action> 
  32.         <!-- 删除影片 --> 
  33.         <actionname="deleteFilm"class="filmAction"method="deleteFilm"> 
  34.             <resultname="success"type="redirectAction">findFilm</result> 
  35.         </action> 
  36.          
  37.     </package> 
  38.      
  39. </struts> 

6、在WebRoot(页面路径下)创建文件夹manager,在manager下创建3个jsp文件:

  • films.jsp (查询页面)
  • insertFilm.jsp (添加影片页面)
  • updateFilm.jsp (修改影片页面)

6.1)films.jsp代码如下:

Html代码  收藏代码
  1. <%@ page language="java"contentType="text/html; charset=utf-8" 
  2. pageEncoding="utf-8" %> 
  3. <%@taglib uri="/struts-tags"prefix="s" %> 
  4. <
  5. String path = request.getContextPath(); 
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  7. %> 
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  9. <html> 
  10.   <head> 
  11.     <title>信息操作</title> 
  12.   </head> 
  13.   <body> 
  14.     <s:formaction="/film/findFilm"method="post"> 
  15.         <s:submitvalue=" 获取所有影片信息 "></s:submit> 
  16.     </s:form> 
  17.     <a href="<%=basePath %>manager/insertFilm.jsp">添加影片信息</a><br/> 
  18.     <s:if test="filmList != null"> 
  19.         <tableborder="1"width="40%"> 
  20.             <tr> 
  21.                 <th>序号</th><th>影片名</th><th>操作</th> 
  22.             </tr>  
  23.             <%-- 遍历影片信息 --%> 
  24.             <s:iteratorvar="film"value="filmList"status="st"> 
  25.                 <tr> 
  26.                     <td><s:propertyvalue="#st.index+1"/></td> 
  27.                     <td><s:propertyvalue="fname"/></td> 
  28.                     <td> 
  29.                         <s:urlid="detailUrl"value="/film/detailFilm"> 
  30.                                         <s:paramname="id"value="%{id}"/> 
  31.                                     </s:url> 
  32.                         <s:ahref="%{detailUrl}">[修改]</s:a>&nbsp; 
  33.                         <s:urlid="deleteUrl"value="/film/deleteFilm"> 
  34.                                         <s:paramname="id"value="%{id}"/> 
  35.                                     </s:url> 
  36.                         <s:ahref="%{deleteUrl}">[删除]</s:a> 
  37.                     </td> 
  38.                 </tr> 
  39.             </s:iterator> 
  40.         </table> 
  41.     </s:if> 
  42.   </body> 
  43. </html> 

6.2)insertFilm.jsp代码如下:

Html代码  收藏代码
  1. <%@ page language="java"contentType="text/html; charset=utf-8" 
  2. pageEncoding="utf-8" %> 
  3. <%@taglib uri="/struts-tags"prefix="s" %> 
  4. <
  5. String path = request.getContextPath(); 
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  7. %> 
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  9. <html> 
  10.   <head> 
  11.     <title>添加影片</title> 
  12.   </head> 
  13.   <body> 
  14.     <s:formaction="/film/insertFilm"method="post"> 
  15.         <s:textfieldname="fname"value=""/> 
  16.         <s:submitvalue=" 添加 "></s:submit> 
  17.     </s:form> 
  18.   </body> 
  19. </html> 

6.3)updateFilm.jsp代码如下:

Html代码  收藏代码
  1. <%@ page language="java"contentType="text/html; charset=utf-8" 
  2. pageEncoding="utf-8" %> 
  3. <%@taglib uri="/struts-tags"prefix="s" %> 
  4. <
  5. String path = request.getContextPath(); 
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  7. %> 
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  9. <html> 
  10.   <head> 
  11.     <title>修改影片</title> 
  12.   </head> 
  13.   <body> 
  14.     <s:formaction="/film/updateFilm"method="post"> 
  15.         <s:hiddenname="id"/> 
  16.         <s:textfieldname="fname"/> 
  17.         <s:submitvalue=" 修改 "></s:submit> 
  18.     </s:form> 
  19.   </body> 
  20. </html>
0 0
原创粉丝点击