Struts2入门案列及其源码分析

1. 目录结构

clip_image001

源码文件:2个:Product对象映射文件,FilterDispatcher过滤器文件

Jsp文件3个,detail细节处理文件,input信息输入文件,index首页文件

配置文件:最重要的文件web.xml,没有这个配置文件,无法实现各种反射效果

2. 各文件源码

过滤器文件源代码

clip_image003

package com.lxgyx.struts2.helloworld;

import javax.servlet.*;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

public class FilterDispatcher implements Filter {

public FilterDispatcher() {

}

@Override

public void init(FilterConfig filterConfig) throws ServletException {

}

@Override

public void doFilter(ServletRequest servletRequest, ServletResponse

servletResponse, FilterChain filterChain) throws IOException,

ServletException {

HttpServletRequest request= (HttpServletRequest) servletRequest;

HttpServletResponse response= (HttpServletResponse) servletResponse;

//1获取servletPath

String servletPath=request.getServletPath();

System.out.println(servletPath);

String path=null;

//2判断ServletPath,若其等于”/product-input.action”,这转发到

///WEB-INF/pages/input.jsp

if (“/product-input.action”.equals(servletPath)){

path=”/WEB-INF/pages/input.jsp”;

}

//若其等于”/product-save.action”,则

if (“/product-save.action”.equals(servletPath)){

//获得请求参数

String productName=request.getParameter(“productName”);

String productDesc=request.getParameter(“productDesc”);

String productPrice=request.getParameter(“productPrice”);

//把请求信息封装为一个 Product对象

Product product=new Product(null,productName,productDesc,

Double.parseDouble(productPrice));

//执行保存操作

System.out.println(“Save Product:”+product);

product.setProductId(1001);

//把对象保存到request中.. ${param.productName} -> ${requestScope.product.productName}. ${param.productName} -> ${requestScope.product.productName}

request.setAttribute(“product”,product);

path=”/WEB-INF/pages/detail.jsp”;

}

if (path!=null){

request.getRequestDispatcher(path).forward(request,response);

return;

}

filterChain.doFilter(request,response);

}

@Override

public void destroy() {

}

}

Product映射文件源代码

clip_image005

package com.lxgyx.struts2.helloworld;

public class Product {

private Integer productId;

private String productName;

private String productDesc;

private double productPrice;

public Product() {

}

public Product(Integer productId, String productName, String productDesc,

double productPrice) {

this.productId = productId;

this.productName = productName;

this.productDesc = productDesc;

this.productPrice = 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 +

‘}’;

}

}

首页index.jsp文件源代码

clip_image007

<%–

Created by IntelliJ IDEA.

User: 18010

Date: 2018/6/10

Time: 20:01

To change this template use File | Settings | File Templates.

–%>

<%@ page contentType=”text/html;charset=UTF-8″ language=”java” %>

<html>

<head>

<title>input</title>

</head>

<body>

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

</body>

</html>

Detail.jsp详情展示文件源代码

clip_image009

<%–

Created by IntelliJ IDEA.

User: 18010

Date: 2018/6/10

Time: 21:05

To change this template use File | Settings | File Templates.

–%>

<%@ page contentType=”text/html;charset=UTF-8″ language=”java” %>

<html>

<head>

<title>detail</title>

</head>

<body>

ProductId:${requestScope.product.productId}

<br>

<br>

ProductName:${requestScope.product.productName}

<br>

<br>

ProductDesc:${requestScope.product.productDesc}

<br>

<br>

ProductPrice:${requestScope.product.productPrice}

<br>

<br>

</body>

</html>

Input输入文件源代码

clip_image011

Web.xml文件源代码

clip_image013

<?xml version=”1.0″ encoding=”UTF-8″?>

<web-app xmlns=”http://xmlns.jcp.org/xml/ns/javaee”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd”

version=”4.0″>

<filter>

<display-name>FilterDispatcher</display-name>

<filter-name>FilterDispatcher</filter-name>

<filter-class>com.lxgyx.struts2.helloworld.FilterDispatcher</filter-class>

</filter>

<filter-mapping>

<filter-name>FilterDispatcher</filter-name>

<url-pattern>*.action</url-pattern>

</filter-mapping>

</web-app>

3. 整体预览

首页

clip_image015

点击链接跳转到信息输入页

clip_image017

点击Submit跳转到信息展示页

clip_image019

通过以上完整案例,实现一个基本的Struts2入门案列效果.

不会的朋友多看看源代码,刚入门的萌新直接将各个目录文件先创建好,然后将源代码复制进去直接运行即可.

建议学习方法:

第一遍:完整复制,实现效果

第二遍:模仿抄写,可以多抄几遍,就像抄作业一样

第三遍:自己理解代码以后开始按照自己的思路和总结,将代码写出来,实现效果,期间有不会的地方可以查看一下源代码.

打赏

发布者

张 大鹏

北京理想国惠玩科技有限公司CEO

发表评论

电子邮件地址不会被公开。 必填项已用*标注