struts部分做的工作比较多
1. web.xml
2. struts.xml
3. action
4. jsp
步骤1:先运行,看到效果,再学习
步骤2:模仿和排错
步骤3:配置web.xml
步骤4:struts.xml
步骤5:Action
步骤6:list.jsp
步骤7:edit.jsp
步骤8:index.jsp
步骤9:测试
步骤 1 : 先运行,看到效果,再学习
老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
访问地址:
http: //127.0.0.1:8080/struts_hibernate/
|
步骤 2 : 模仿和排错
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。
推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
步骤 3 : 配置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 >
|
步骤 4 : 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 >
|
步骤 5 : 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" ;
}
}
|
步骤 6 : 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 >
|
步骤 7 : 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 >
|
步骤 8 : index.jsp
在WebContent目录下创建一个index.jsp,用于跳转到查询product页面
< jsp:forward page = "listProduct" />
|
步骤 9 : 测试
测试访问地址:
http: //127.0.0.1:8080/struts_hibernate/
|
更多内容,点击了解: https://how2j.cn/k/struts-hibernate/struts-hibernate-struts/83.html