Struts2(二)struts.xml详解

1 struts.xml 文件

我们已经知道Struts2 作为控制器(Controller)来建立模型与视图的数据交互。采用拦截器的机制来处理用户的请求。其实它是通过struts.xml 这个中间文件来实现的。

2 struts.xml 文件的书写

① 新建一个web项目,目录结构如图所示,在index.jsp 中有一个input.action 请求:

② 在struts.xml 中应答input.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包: 可以在Web App Libraries的 struts2-core-2.x.x.jar中的
                          struts-default.xml 看到.
        namespace: 可选,若没有给出默认值为: / 
        http://localhost:8080/contextPath/namespace/actionName.action
     -->
    <package name="helloword" extends="struts-default" namespace="/">
        <!-- 
                        配置一个action: 一个struts2 的请求就是一个action
           name: 对应一个struts2 的请求的名字去除扩展名(也可以说是对应一个servletPath,去除/和扩展名)
           class: 默认值为 com.opensymphony.xwork2.ActionSupport
           method: 默认值为 excute
           result: 结果(响应)
                    name: 标识一个result,默认值为success
                    type:结果类型,默认值 dispatcher(转发)
         -->
         <!--  
        <action name="input" class="com.opensymphony.xwork2.ActionSupport" 
        method="excute">
            <result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>
        </action>
        -->
        <action name="input">
            <result>/WEB-INF/pages/input.jsp</result>
        </action>
        
    </package>

</struts>
③ 响应请求,启动tomcat,运行index.xml,点击链接(发送请求),页面就会跳转到 WEB-INF/pages/input.jsp 页面。

3 action 与Action 类

① 在struts.xml 中新增一个action
<action name="save" class="com.test.Product" method="save">
            <result name="details">/WEB-INF/pages/details.jsp</result>  
</action>
② 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="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"/>
		<br><br>
	
	</form>

</body>
</html>
③ Product类
package com.test;

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 + ", productDesc=" + productDesc
				+ ", productPrice=" + productPrice + "]";
	}
	
	public String save(){
		System.out.println("save: " + this);
		return "details";
	}
	
	public Product() {
		System.out.println("Product's constructor...");
	}
	
}
④ detail.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>
	
    ProductName: ${requestScope.product.productName}	//没有值
    <br><br>
    
    ProductName: ^<%= request.getAttribute("productName") %>
    <br><br>
    
    ProductDesc: ${productDesc }
    <br><br>
    
    ProductPrice: ${productPrice }
    <br><br>
    
    
    <%= request %>	//org.apache.struts2.dispatcher.StrutsRequestWrapper@2a242f84
	
</body>
</html>

⑤ 在上面 Product 类 和ActionSupport 类(默认的 Action 类: 若某个 action 节点没有配置 class 属性, 则 ActionSupport 即为
待执行的 Action 类. 而 execute 方法即为要默认执行的 action 方法)都是Action 类.

⑥ action 与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、付费专栏及课程。

余额充值