struts2 基本配置

响应请求最基本的实现。相比实现Filter 来说,使用 struts.xml 文件可以实现更多功能,可以响应web 请求。

struts自带的拦截器也能识别出是哪个servletpath,然后调用struts.xml配置文件配置的action,进而利用返回的result 来进行下一步操作。

1.加入jar包

项目结构:

普通类:

package com.cwh.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 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;
    }

    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() {

    }

    [@Override](https://my.oschina.net/u/1162528)
    public String toString() {
        return "Product [productId=" + productId + ", productName=" + productName + ", productDesc=" + productDesc
                + ", productPrice=" + productPrice + "]";
    }

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

2.struts2配置文件

struts.xml 文件:

<?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>
    <!-- 初次配置需要在Window preferences的catolog 的 File 里
    添加本地的struts-2.3.dtd文件,
    Key type选择 URI,
     Key 为http://struts.apache.org/dtds/struts-2.3.dtd -->

    <!-- 
        package: 包,struts2 使用 package 来组织模块。
        name 属性: 用于别的包应用当前包。
        extends: 指定当前包需要继承的那个包,可以继承那个包中所有的配置。通常配置struts-default
                struts-default 配置有各种结果类型,还有各种拦截器和拦截器栈
        namespace:命名空间,发请求时,URI上项目名后面,请求名前面加上,例如localhost:8080/struts2-2/chen/product-input
     -->
    <package name="helloWorld" extends="struts-default" namespace="/chen">

        <!-- 配置一个action: 一个struts2的请求就是一个action
            name: 对应一个struts2的请求名,不包含扩展名 
            result: 结果  
        -->
        <!-- 如果没有写class, 默认的是ActionSupport 类,如果不写method,默认也是execute 方法,返回success -->
        <action name="product-input">
            <result>/WEB-INF/pages/input.jsp</result>
        </action>

        <!-- 这个节点的写法,效果等同于上一个。如果没有指定类和方法,
            类就默认值:ActionSupport 
            方法默认值:execute 方法,并且返回值为success 
            type:表示结果类型,默认值为 dispatcher (转发结果),还可以取值redirect(重定向)等 -->
        <action name="product-input2" 
            class="com.opensymphony.xwork2.ActionSupport"
            method="execute">
            <result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>
        </action>

        <action name="product-save" class="com.cwh.struts2.helloworld.Product"
            method="save">
            <result name="datails">/WEB-INF/pages/details.jsp</result>
            <result name="datails2">/WEB-INF/pages/input.jsp</result>
        </action>
    </package>


</struts>

初始界面 index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <a href="product-input.action">Product Input2</a>
</body>
</html>

input.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <!-- struts2的一个拦截器栈有一个名为 params 的拦截器,可以获取请求参数,并把请求参数赋值到一个bean 的
	 同名set 方法的入参上,然后通过set 方法给属性赋值赋值.例如此处表单字段名为 "productName2",就需要有setProductName2 方法) -->
    <form action="product-save.action" method="post">
    <br>
        ProductName: <input type="text" name="productName2"/>
        <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>
    </a>
</body>
</html>

 

detail.jsp:

当发送product-save 请求时,控制台会打印出对象信息。这就说明了 input.jsp 提交的请求参数被写到对应的bean 上,并且放在请求域里。

所以下面request 请求域同样能拿到请求参数(不推荐这种写法,推荐直接使用${param} 写法)

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

    ProductId: ${productId}
    <br>
    ProductId: ^<%= request.getAttribute("productName") %>
    <br>
    ProductName: ${productName}
    <br>
    ProductDesc: ${productDesc}
    <br>
    ProductPrice: ${productPrice}

</body>
</html>

3.配置Filter

* StrutsPrepareAndExecuteFilter 的配置一般都不会有改动,所以不要求记,可以复制完事

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_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <!-- 配置 Struts2 的 Filter (不再需要自己实现 Filter 然后写 doFilter 方法) -->
    <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>

转载于:https://my.oschina.net/u/3780366/blog/1920178

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值