SSH框架學習(三)——struts整合spring
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整合成功。