Struts2学习计划(二)——使用struts机制还原

准备工作

所需jar包

首先,将jar包直接放在lib目录下,并且在web.xml设置struts2配置

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">

	<!-- 配置 Struts2 的 Filter -->
	<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>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>

文件目录

前段代码

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>

	<a href="testStruts2/product-input.action">Product Input</a>

</body>
</html>

details.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
	ProductId : ${productId }
	<br><br>
	ProductName : ${productName }
	<br><br>
	ProductDesc : ${productDesc }
	<br><br>
	ProductPrice : ${productPrice }
	<br><br>
</body>
</html>

input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
	<form action="product-save.action" method="post">
		ProductName:<input type="text" name="productName">
		<br><br>
		ProductDesc:<input type="text" name="productDesc">
		<br><br>
		ProductPrice:<input type="text" name="productPrice">
		<br><br>
		<input type="submit" value="submit">
	</form>
</body>
</html>

实体类

package com.struts.environment;

public class Product {
	private Integer productId;
	private String productName;
	private String productDesc;
	private double productPrice;

	public Integer getProductId() {
		return productId;
	}

	public void setProductId(Integer productId) {
		this.productId = productId;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getProductDesc() {
		return productDesc;
	}

	public void setProductDesc(String productDesc) {
		this.productDesc = productDesc;
	}

	public double getProductPrice() {
		return productPrice;
	}

	public void setProductPrice(double productPrice) {
		this.productPrice = productPrice;
	}

	@Override
	public String toString() {
		return "Product [productId=" + productId + ", productName=" + productName + ", produceDesc=" + productDesc
				+ ", productPrice=" + productPrice + "]";
	}

	public String save() {
		System.out.println("Save :" + this);
		return "details";
	}

}

struts配置

   <?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包,struts2使用package来组织模块, name属性,必须,用于其他包应用当前包 extends:当前包继承那个包,可继承其中所有的配置,通常情况下继承struts-default 
		extends 和 interface 又有着什么样子的不同 -->
	<package name="helloWorld" extends="struts-default" namespace="/testStruts2">
		<!-- 配置一个action,一个struts2的请求就是一个action name:对应一个struts2的请求名字(或对一个servletPath 
			但去除/和扩展名),不包括扩展名 result:结果 默认的结果是success -->
		<action name="product-input">
			<result name="huhu">/WEB-INF/pages/koko.jsp</result>
			<result>/WEB-INF/pages/input.jsp</result>
		</action>
		<!-- 默认的class为ActionSupport类 -->
		<action name="product-save" class="com.struts.environment.Product"
			method="save">
			<result name="details">/WEB-INF/pages/details.jsp</result>
			<result name="input">/WEB-INF/pages/strttt.jsp</result>
		</action>
		<action name="product_*" class="com.struts.environment.Product"
			method="{1}">
			<result name="ref">/WEB-INF/pages/koko.jsp</result>
		</action>


	</package>
</struts>


node.txt

1.VS 自实现:
1)搭建Struts2的开发环境
2)不需要显示的定义filter,而是用的是struts2的配置文件
3)details.jsp比以前更简单
4)${requestScope.product.productName} -> ${productName}

4) 步骤:

	1.点击action进入strtus.xml文件中,然后进入相应的class寻找相应的方法
	2.获得相应的方法返回值之后,返回相应的jsp页面或者是其他路径


posted @ 2017-07-16 18:36  startor  阅读(91)  评论(0)    收藏  举报