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>
<?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框架去创建