2.Struts2_HelloWorld

本文详细介绍了如何使用Struts2框架对Product类进行操作,包括无需使用ActionForm封装请求参数、避免使用Servlet API、在页面中使用OGNL显示对象模型等特性,并通过配置web.xml和struts.xml实现Action的定义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用上篇Product的例子,使用Struts2框架来实现。

前台两个页面改动不大,参见第一节。


Struts2 VSStruts1

1.Struts2无需使用ActionForm 来封装请求参数。

2.没有使用Servlet的API

3.struts2在页面中使用OGNL来显示各种对象模型,可以不适用EL和JSTL表达式

---------------------------------------------------------------------------------------------------------------------------------------------

1.Product类

类中添加了Save()方法

package com.hcx.struts2.helloworld;

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 getProductDesc() {
		return productDesc;
	}

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

	public String getProductName() {
		return productName;
	}

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

	public double getProductPrice() {
		return productPrice;
	}

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

	
	public Product(Integer productId, String productName, String productDesc,
			double productPrice) {
		super();
		this.productId = productId;
		this.productName = productName;
		this.productDesc = productDesc;
		this.productPrice = productPrice;
	}

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


2.web.xml配置struts2框架


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!--  配置 struts2过滤器-->
  <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>


3.在src目录下建立struts.xml配置action



<?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
		         struts-default 这个包在 struts-default.xml 文件中定义.
		namespace 可选, 如果它没有给出, 则以 / 为默认值. 
		                        若 namespace 有一个非默认值, 则要想调用这个包里的Action, 
		                        就必须把这个属性所定义的命名空间添加到有关的 URI 字符串里
		                        
		          http://localhost:8080/contextPath/namespace/actionName.action
	-->
    <package name="helloWorld" extends="struts-default">
    	
    	<!-- 
    		配置一个 action: 一个 struts2 的请求就是一个 action 
    		name: 对应一个 struts2 的请求的名字(或对一个 servletPath, 但去除 / 和扩展名), 不包含扩展名
    		class 的默认值为: com.opensymphony.xwork2.ActionSupport
    		method 的默认值为: execute
    		result: 结果. 
    	-->
    	<action name="product-input" 
    		class="com.opensymphony.xwork2.ActionSupport"
    		method="execute">
    		<!--  
    			result: 结果. 表示 action 方法执行后可能返回的一个结果. 所以一个 action 节点可能会有多个 result 子节点.
    			多个 result 子节点使用 name 来区分
    			name: 标识一个 result. 和 action 方法的返回值对应. 默认值为 success
    			type: 表示结果的类型. 默认值为 dispatcher(转发到结果.)
    		-->
    		<result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>
    	</action>
    	
    	<action name="product-save" class="com.hcx.struts2.helloworld.Product"
    		method="save">
    		<result name="details">/WEB-INF/pages/details.jsp</result>	
    	</action>
    	
    	<action name="test" class="com.hcx.struts2.helloworld.Product" method="test">
    		<result>/index.jsp</result>
    	</action>
    	
    </package>

</struts>

2. action VS Action 类



1). action: 代表一个  Struts2 的请求. 


2). Action 类: 能够处理 Struts2 请求的类. 


> 属性的名字必须遵守与 JavaBeans 属性名相同的命名规则. 
   属性的类型可以是任意类型. 从字符串到非字符串(基本数据库类型)之间的数据转换可以自动发生


> 必须有一个不带参的构造器: 通过反射创建实例 

> 至少有一个供 struts 在执行这个 action 时调用的方法

> 同一个 Action 类可以包含多个 action 方法. 

> Struts2 会为每一个 HTTP 请求创建一个新的 Action 实例, 即 Action 不是单例的, 是线程安全的. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值