目录
0、Struts部分
struts部分做的工作比较多
1. web.xml
2. struts.xml
3. action
4. jsp
1、先运行,看到效果,再学习
老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
访问地址:
http://127.0.0.1:8080/struts_hibernate/
2、配置web.xml
配置struts专用过滤器
<web-app>
<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>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3、struts.xml
分别为增加,删除,获取,修改,查询配置Action
为了便于理解,这里没有使用通配符,可以查看struts章节的通配符匹配简化配置
<?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.i18n.encoding" value="UTF-8"></constant>
<package name="basicstruts" extends="struts-default">
<action name="addProduct" class="com.how2java.action.ProductAction"
method="add">
<result name="list" type="redirect">listProduct</result>
</action>
<action name="deleteProduct" class="com.how2java.action.ProductAction"
method="delete">
<result name="list" type="redirect">listProduct</result>
</action>
<action name="editProduct" class="com.how2java.action.ProductAction"
method="edit">
<result name="edit">/product/edit.jsp</result>
</action>
<action name="updateProduct" class="com.how2java.action.ProductAction"
method="update">
<result name="list" type="redirect">listProduct</result>
</action>
<action name="listProduct" class="com.how2java.action.ProductAction"
method="list">
<result name="listJsp">/product/list.jsp</result>
</action>
</package>
</struts>
4、Action
分别为增加,删除,修改,查询,获取准备对应的方法.
声明实例化ProductDAO pdao = new ProductDAO();以便于在每一个方法中调用
package com.how2java.action;
import java.util.List;
import com.how2java.dao.ProductDAO;
import com.how2java.pojo.Product;
public class ProductAction {
ProductDAO pdao = new ProductDAO();
Product product;
List<Product> products;
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public String add() {
pdao.add(product);
return "list";
}
public String edit() {
product =pdao.get(product.getId());
return "edit";
}
public String delete() {
pdao.delete(product.getId());
return "list";
}
public String update() {
pdao.update(product);
return "list";
}
public String list() {
products = pdao.listProduct();
return "listJsp";
}
}
5、list.jsp
在web目录下,新建一个product目录,接着在product目录下创建list.jsp文件
list.jsp同时提供增加和显示功能
以及删除和修改的超链
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
<table align="center" border="1" cellspacing="0" width="500px">
<tr>
<td>id</td>
<td>name</td>
<td>price</td>
<td>edit</td>
<td>delete</td>
</tr>
<s:iterator value="products" var="p">
<tr>
<td>${p.id}</td>
<td>${p.name}</td>
<td>${p.price}</td>
<td><a href="editProduct?product.id=${p.id}">edit</a></td>
<td><a href="deleteProduct?product.id=${p.id}">delete</a></td>
</tr>
</s:iterator>
</table>
<br/>
<form action="addProduct" method="post">
<table align="center" border="1" cellspacing="0">
<tr>
<td>
name:
</td>
<td>
<input type="text" name="product.name" value="">
</td>
</tr>
<tr>
<td>
price:
</td>
<td>
<input type="text" name="product.price" value="0">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
6、edit.jsp
在product目录下创建edit.jsp文件,用于编辑
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
<form action="updateProduct" method="post">
<table align="center" border="1" cellspacing="0">
<tr>
<td>
name:
</td>
<td>
<input type="text" name="product.name" value="${product.name}">
</td>
</tr>
<tr>
<td>
price:
</td>
<td>
<input type="text" name="product.price" value="${product.price}">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="product.id" value="${product.id}">
<input type="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
7、index.jsp
在WebContent目录下创建一个index.jsp,用于跳转到查询product页面
<jsp:forward page="listProduct"/>
8、测试
测试访问地址:
http://127.0.0.1:8080/struts_hibernate/