Struts2-3 HelloWorld详解

来源:互联网 发布:mysql的配置 编辑:程序博客网 时间:2024/05/20 21:43

一、HelloWorld的实现

 基于Struts2方式与自实现Filter方式做MVC控制器的区别在于:

  1. 需要搭建Struts2开发环境;
  2. 不需要显式定义Filter,而使用的是Struts2的配置文件来实现;
  3. details.jsp更为简单,即由${requestScope.product.productName}--> ${productName},此处是依靠Struts2的值栈来实现的;
  4. Product类可以不需要创建带参的构造器,依靠params拦截器来实现请求参数名与实体类getter()与setter()方法的映射;
  5. 实现步骤:
    (1). 由product-input.action转到/WEB-INF/pages/input.jsp;
    (2). 由input.jsp页面的product-save.action到Product类的save()方法,再到/WEB-INF/pages/details.jsp;
    (3). 在Product类中定义save()方法,且返回值为details。

    配置文件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>    <!--         package: 包,Struts2使用package来组织模块(不同的功能采用不同的模块来组织).        name属性:必备,用于其他的包来引用当前包.        extends属性:当前包所继承的包,继承的,即可以继承其中的所有配置.        通常情况下继承于struts2-core-2.3.31.jar包中struts-default.xml的struts-default包.        namespace属性:可选的,默认值为“/”. 若指定该属性值,则在调用该包中的action时,需要将该属性值添加到对应的URI字符串中,即如http://localhost:8080/contextPath/namespace/actionName.action     -->     <package name="struts2-helloworld" extends="struts-default">        <!--             配置action:一个Struts2的请求即为一个action.            name属性:对应struts2请求的名称,但注意不包含“.action”.            class属性:默认值为"com.opensymphony.xwork2.ActionSupport"            method属性:默认值为"execute"            result属性: 结果.         -->        <action name="product-input">            <!--                 result: 表示action方法执行后可能返回的一个结果                name属性:与action方法的返回值对应,默认值为success                type属性:结果类型,默认值为dispatcher(转发到结果)             -->            <result>/WEB-INF/pages/input.jsp</result>        </action>        <action name="product-save" class="com.qiaobc.struts2.domain.Product" method="save">            <result name="details">/WEB-INF/pages/details.jsp</result>        </action>     </package></struts>

二、解决输入中文后页面上显示乱码的问题?

  需要在product-input.action请求前,配置拦截器,来设置request的编码方式。


附录:具体实现代码

代码结构:

这里写图片描述

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="product-input.action">Product Input...</a></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">        <table>            <tr>请添加商品信息:</tr>            <tr>                <td>ProductName:</td>                <td><input type="text" name="productName"/></td>            </tr>            <tr>                <td>ProductDesc:</td>                <td><input type="text" name="productDesc"/></td>            </tr>            <tr>                <td>ProductPrice:</td>                <td><input type="text" name="productPrice"/></td>            </tr>            <tr>                <td><input type="submit" value="Submit"/></td>            </tr>        </table>    </form></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>    The Product has been saved...<a href="index.jsp">Return</a>    <table>        <tr>            <td>ProductID:</td>         <td>${productId }</td>        </tr>        <tr>            <td>ProductName:</td>       <td>${productName }</td>        </tr>        <tr>            <td>ProductDesc:</td>       <td>${productDesc }</td>        </tr>        <tr>            <td>ProductPrice:</td>      <td>${productPrice }</td>        </tr>    </table>    </body></html>

Product.java:

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;    }    // 在Struts2中可去除带参构造器    @Override    public String toString() {        return "Product [productId=" + productId + ", productName="                + productName + ", productDesc=" + productDesc                + ", productPrice=" + productPrice + "]";    }    public String save() {        System.out.println("save:" + this);        return "details";    }   }
0 0
原创粉丝点击