欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MyBatis 3章 MyBatis Spring Struts2 整合应用

程序员文章站 2022-07-13 16:58:32
...

 

MyBatis 2章 MyBatis与Spring整合

使用Spring Security实现权限管理

使用ajax gson增强用户体验

 

MyBatis 3章 MyBatis Spring Struts2 整合应用

 

 

 

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 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-2.5.xsd">

	<!-- 这里配置Action -->
	
</beans>

 

 

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

 

 

<?xml version="1.0" encoding="UTF-8" ?>
	<!DOCTYPE struts PUBLIC
	    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	    "http://struts.apache.org/dtds/struts-2.0.dtd">
	
	<struts>
		<constant name="struts.enable.DynamicMethodInvocation" value="true" />
		<constant name="struts.devMode" value="true" />
		<constant name="struts.i18n.encoding" value="UTF-8" />
		<constant name="struts.objectFactory" value="spring" />
		<constant name="struts.objectFactory.spring.autoWire" value="type" />
		<constant name="struts.ui.theme" value="simple"></constant>
		<package name="film" namespace="/film" extends="struts-default">
			<!-- 设置Action -->
		</package>
</struts>

 

 

 

2.5)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>
	  
	  	<context-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
		</context-param>
		
		<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>
			<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
		</listener>
	  
	</web-app>

 

 

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

 

 

package com.xxx.web.struts.action;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.xxx.pojo.Film;
import com.xxx.service.FilmService;

@SuppressWarnings("serial")
public class FilmAction extends ActionSupport implements ModelDriven<Film> {

	//业务逻辑对象
	@Autowired
	private FilmService filmService;
	
	//封装查询结果
	private List<Film> filmList;
	
	//封装页面提交的表单数据
	private Film film = new Film();
	
	/**
	 * 获取所有的电影
	 * @return
	 * @throws Exception
	 */
	public String findFilm() throws Exception {
		
		this.filmList = this.filmService.getAllFilm();
		return SUCCESS;
	}
	
	/**
	 * 根据影片ID获取影片信息
	 * @return
	 * @throws Exception
	 */
	public String detailFilm() throws Exception {

		int id = film.getId().intValue();
		Film film = this.filmService.getFilmById(id);
		this.film.setFname(film.getFname());
		return SUCCESS;
	}
	
	/**
	 * 添加影片
	 * @return
	 * @throws Exception
	 */
	public String insertFilm() throws Exception {

		this.filmService.insertFilm(film);
		return SUCCESS;
	}
	
	/**
	 * 修改影片
	 * @return
	 * @throws Exception
	 */
	public String updateFilm() throws Exception {

		this.filmService.updateFilm(film);
		return SUCCESS;
	}
	
	/**
	 * 删除影片
	 * @return
	 * @throws Exception
	 */
	public String deleteFilm() throws Exception {

		int id = film.getId().intValue();
		this.filmService.deleteFilm(id);
		return SUCCESS;
	}
	
	public Film getModel() {
		return film;
	}

	public List<Film> getFilmList() {
		return filmList;
	}

	public void setFilmList(List<Film> filmList) {
		this.filmList = filmList;
	}

}

 

 

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

 

 

<?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-2.5.xsd">

	<!-- 创建FilmAction -->
	<bean id="filmAction"	
		class="com.xxx.web.struts.action.FilmAction"
		scope="prototype"/>
	
</beans>

 

 

5、在struts.xml中配置Action

 

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<constant name="struts.devMode" value="true" />
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<constant name="struts.objectFactory" value="spring" />
	<constant name="struts.objectFactory.spring.autoWire" value="type" />
	<constant name="struts.ui.theme" value="simple"></constant>
	
	<package name="film" namespace="/film" extends="struts-default">
		
		<!-- 获取所有影片 -->
		<action name="findFilm" class="filmAction" method="findFilm">
			<result name="success">/manager/films.jsp</result>
		</action>
		<!-- 添加影片 -->
		<action name="insertFilm" class="filmAction" method="insertFilm">
			<result name="success" type="redirectAction">findFilm</result>
		</action>
		<!-- 获取影片详情 -->
		<action name="detailFilm" class="filmAction" method="detailFilm">
			<result name="success">/manager/updateFilm.jsp</result>
		</action>
		<!-- 修改影片 -->
		<action name="updateFilm" class="filmAction" method="updateFilm">
			<result name="success" type="redirectAction">findFilm</result>
		</action>
		<!-- 删除影片 -->
		<action name="deleteFilm" class="filmAction" method="deleteFilm">
			<result name="success" type="redirectAction">findFilm</result>
		</action>
		
	</package>
	
</struts>

 

 

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

 

 

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

 

6.1)films.jsp代码如下:

 

 

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" %>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
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>
    <title>信息操作</title>
  </head>
  <body>
    <s:form action="/film/findFilm" method="post">
    	<s:submit value=" 获取所有影片信息 "></s:submit>
    </s:form>
    <a href="<%=basePath %>manager/insertFilm.jsp">添加影片信息</a><br />
    <s:if test="filmList != null">
    	<table border="1" width="40%">
    		<tr>
    			<th>序号</th><th>影片名</th><th>操作</th>
    		</tr>	
	    	<%-- 遍历影片信息 --%>
	    	<s:iterator var="film" value="filmList" status="st">
		    	<tr>
		    		<td><s:property value="#st.index+1" /></td>
		    		<td><s:property value="fname" /></td>
		    		<td>
		    			<s:url id="detailUrl" value="/film/detailFilm">
			                      		<s:param name="id" value="%{id}"/>
			                      	</s:url>
		    			<s:a href="%{detailUrl}">[修改]</s:a>&nbsp;
		    			<s:url id="deleteUrl" value="/film/deleteFilm">
			                      		<s:param name="id" value="%{id}"/>
			                      	</s:url>
		    			<s:a href="%{deleteUrl}">[删除]</s:a>
		    		</td>
		    	</tr>
			</s:iterator>
		</table>
    </s:if>
  </body>
</html>

 

 

6.2)insertFilm.jsp代码如下:

 

 

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" %>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
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>
    <title>添加影片</title>
  </head>
  <body>
    <s:form action="/film/insertFilm" method="post">
    	<s:textfield name="fname" value="" />
    	<s:submit value=" 添加 "></s:submit>
    </s:form>
  </body>
</html>

 

6.3)updateFilm.jsp代码如下:

 

 

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" %>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
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>
    <title>修改影片</title>
  </head>
  <body>
    <s:form action="/film/updateFilm" method="post">
    	<s:hidden name="id" />
    	<s:textfield name="fname" />
    	<s:submit value=" 修改 "></s:submit>
    </s:form>
  </body>
</html>

 


提示:本文示例项目已提供下载

 

MyBatis 2章 MyBatis与Spring整合

使用Spring Security实现权限管理

使用ajax gson增强用户体验