SSH框架学习(三)——struts整合spring


以商品管理系统为例,编写保存商品操作体现整合过程。

创建包结构

项目需要创建如下包结构:

  • action
  • dao
  • domain
  • service

struts整合spring

创建提交页面

在WebRoot下新建addProduct.jsp

<%@ page language="java" import="java.util.*" 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>addProduct page</title>
  </head>
  
  <body>
  	添加商品页面<br>
  	<s:form action="product_save" method="post" namespace="/" theme="simple">
  		<table>
  			<tr>
  				<td>商品名称</td>
  				<td><s:textfield name="pname"></s:textfield></td>
  			</tr>
  			<tr>
  				<td>商品价格</td>
  				<td><s:textfield name="price"></s:textfield></td>
  			</tr>
  			<tr>
  				<td colspan="2" align="center"><s:submit value="提交"></s:submit></td>
  			</tr>
  		</table>
  	</s:form>
  </body>
 
</html>

注意:
1、<%@ taglib uri="/struts-tags" prefix=“s” %> 使用struts2的标签库布局web页面
2、提交路径 action=“product_save” 具体与action的处理操作有关,与struts和spring整合的了两种方式中的struts.xml或是application.xml的配置对应。
3、theme=“simple”,即不采用struts2的默认标签

编写domain

在domain包下创建Product.java,即商品管理的实体类。
product.java

package cn.domain;
/**
 * 商品管理的实体类
 */
public class product {
	private Integer pid;
	private String pname;
	private double price;
	
	public Integer getPid() {
		return pid;
	}
	public void setPid(Integer pid) {
		this.pid = pid;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
}

注意:
1、要对实体编写get和set的方法。

编写dao

在dao包下创建ProductDao.java,即商品管理的dao类

package cn.ssh.dao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.ssh.domain.Product;

/**
 * 商品管理dao类
 */
public class ProductDao {

	// 保存商品
	public void save(Product product) {
		System.out.print("dao执行了");
	}
}

编写service

在service包下创建ProductService.java,即商品管理的业务层的类,也可以创建接口

package cn.ssh.service;

import org.springframework.transaction.annotation.Transactional;
import cn.ssh.dao.ProductDao;
import cn.ssh.domain.Product;

/**
 * 商品管理的业务层
 */
@Transactional
public class ProductService {
	
	//提供set方法,在spring中有set方法就可以注入,自动注入dao的类
	private ProductDao productDao;
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}
	
	// 业务层保存商品
	public void save(Product product) {
		System.out.println("service执行了");
		productDao.save(product);
		
	}
}

注意:
1、在spring中有set方法就可以自动注入,在service中需要注入dao的类,dao交由service管理。

编写action

在action包下创建ProductAction.java,即商品管理的action类


package cn.ssh.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.ssh.domain.Product;
import cn.ssh.service.ProductService;

/**
 * 商品管理的action
 */
 //action需要继承ActionSupport,实现ModelDriven接口
public class ProductAction extends ActionSupport implements ModelDriven<Product>{
	//模型驱动的类
	private Product product=new Product();
	
	public Product getModel(){
		return product;
	}
	
	//service自动注入业务层的类
	private ProductService productService;

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	//保存商品执行方法:save
	public String save() {
		System.out.println("action执行了");
		productService.save(product);
		//页面不需要跳转
		return NONE;
	}
}

注意:
1、在spring中有set方法就可以自动注入,在action中需要注入service的类,可以免去创建工厂。

配置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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans    
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
      http://www.springframework.org/schema/context   
      http://www.springframework.org/schema/context/spring-context-2.5.xsd   
      http://www.springframework.org/schema/aop   
      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
      http://www.springframework.org/schema/tx   
      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	<!-- 配置业务层的类 -->
	<bean id="productService" class="cn.ssh.service.ProductService">
		<!-- dao交由service管理,注入dao -->
		<property name="productDao" ref="productDao">
		</property>
	</bean>
	
	<!-- 配置dao的类 -->
	<bean id="productDao" class="cn.ssh.dao.ProductDao">
	</bean>
	

配置struct2.xml

struct2和spring整合的两种方式

方法一:action的类在struts.xml中创建

struts.xml

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

<struts>
	<package name="ssh" extends="struts-default" namespace="/">
		<action name="product_*" class="cn.ssh.action.productAction" method="{1}"></action>
	</package>

</struts>

注意:
1、class=“cn.ssh.action.productAction” 中需要填写action的全路径

方法二:在application.xml中配置
需要在application.xml原来的基础上配置action
application.xml

<!-- 配置action的类 -->
	<bean id="productAction" class="cn.ssh.action.ProductAction" scope="prototype">
		<!-- 交给spring管理需要手动注入service,scope="prototype"为多例创建 -->
		<property name="productService" ref="productService">
		</property>
	</bean>

修改struts.xml
struts.xml

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

<struts>
	<package name="ssh" extends="struts-default" namespace="/">
		<action name="product_*" class="productAction" method="{1}"></action>
	</package>

</struts>

注意:
1、class=“productAction” 中class与application.xml中action配置的id对应

整合成功结果

运行程序,进入提交商品页面
在这里插入图片描述

点击提交,可以看到控制台输出
在这里插入图片描述

即说明struts和spring整合成功。

猜你喜欢

转载自blog.csdn.net/qq_35302274/article/details/84727585
今日推荐