Struts和Spring整合的两种方式

1.Action的类由Struts2自身去创建

jsp代码,注意taglib需要加上,否则JSP页面无法识别标签

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>保存商品的页面</title>
</head>
<body>
	<h1>保存商品的页面</h1>
	<s:form action="product_save" method="post" namespace="/" theme="simple">
		<table border="1" width="400">
			<tr>
				<td>商品名称</td>
				<td width="300"><textarea name="pname" rows="1"></textarea></td>
			</tr>
			<tr>
				<td>商品价格</td>
				<td width="300"><textarea name="price" rows="1"></textarea></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="添加" width="100" ></td>
			</tr>
		</table>

	</s:form>

</body>
</html>

Strust2配置

<?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="com.muke.ssh.action.ProductAction" method="{1}"></action>
</package>

</struts>


Spring配置文件

<?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-4.1.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/aop/spring-tx-4.1.xsd
        ">

	<!-- 配置DAO的类 -->
	<bean id="productDao" class="com.muke.ssh.dao.ProductDao"></bean>
	<!-- 配置业务层类 -->
	<bean id="productService" class="com.muke.ssh.service.ProductService">
		<property name="productDao" ref="productDao"></property>
	</bean>

</beans>

需要注意的是:

ProductAction的代码:

public class ProductAction extends ActionSupport  implements ModelDriven<Product> {
	
	//模型驱动使用的类
	public Product product=new Product();
	@Override
	public Product getModel(){
		return product;
	}
	
	//Struts和Spring整合中按名称自动注入业务层类<pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">                public ProductService productService;</span><pre name="code" class="java">        public void setProductService(ProductService productService){
		this.productService=productService;
		
	}
		
	/**
	 * 保存商品的执行方法:save
	 */
	public String save(){
		System.out.println("ProductAction中的save方法执行了。。。。。");
		productService.save(product);
		return NONE;
	}

}

 
 


2.Action的类由Spring框架去创建

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值