Struts2框架入门(一)

Struts2框架介绍

Struts2是一个基于MVC设计模式的Web应用框架,在Struts1和WebWork的技术基础进行了合并。采用拦截器的机制来处理用户的请求,使得业务逻辑能够与Servlet API完全脱离开。

Struts2核心功能

1.允许POJO对象作为Action

2.Action的execute方法不再与Servlet API耦合,更易测试

3.支持更多视图技术(JSP、FreeMarker、Velocity)

4.基于Spring AOP思想的拦截器机制,更易扩展

Struts2核心组件

FC 前端控制器,负责统一的分发请求
Action 业务控制器,负责处理某一类业务
ValueStack Action与JSP交互的媒介
Interceptor 拦截器,负责扩展Action,处理Action的共通事务
Result 负责输出的组件
Tags 标签,负责显示数据,生成框体

代码实现

本篇Demo基于Struts2.5.16,JDK1.8

GitHub地址:https://github.com/The-ProgramLife/Demo/tree/master/Struts2

第一个Struts2应用

实现在网页上显示Hello World!

创建一个Java Maven Project

在pom.xml引入Struts2依赖

注:因为使用maven创建WebAPP,默认会报错,还需要引入Servlet依赖

		<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.5.16</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
			<scope>provided</scope>
		</dependency>

修改web.xml,配置一个Filter,所有的请求默认都会被过滤给这个Filter

<web-app id="starter" 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">
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
		</filter-class>
	</filter>
	
	<filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

在src/main/java下创建struts.xml文件,负责控制器与试图的映射

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
	<!-- 使用默认的一套拦截器 -->
	<package name="basicstruts" extends="struts-default">

		<!-- 默认访问路径 -->
		<action name="">
			<result>index.jsp</result>
		</action>


	</package>
</struts>    

修改index.jsp,这就是视图

<html>
<head>
<title>index</title>
</head>
<body>
Hello World!
</body>
</html>

部署在Tomcat中,然后启动Tomcat,访问地址http://localhost:8080/Struts2/就能看到效果

把模型的数据显示到视图中

在src/main/java下创建POJO类Product.java,用于存放数据(Model层)

package model;
 
public class Product {
 
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

创建ProductAction类用于控制Model与View的数据交互(Controller层)

package controller;
 
import model.Product;
 
public class ProductAction {
    private Product product;
 
    public String show() {
        product = new Product();
        product.setName("mi5");
        return "show";
    }
 
    public Product getProduct() {
        return product;
    }
 
    public void setProduct(Product product) {
        this.product = product;
    }
}

修改struts.xml

添加showProduct路径

		<!-- 访问路径/showProduct会调用 ProductAction 类的 show 方法,服务端跳转到show.jsp  -->
		<action name="showProduct" class="controller.ProductAction"
			method="show">
			<result name="show">show.jsp</result>
		</action>

在src/main/webapp下创建 show.jsp

通过EL表达式,取出product的name属性

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="s" uri="/struts-tags"%>    
<!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>ShowProduct</title>
</head>
<body>
${product.name}
</body>
</html>

部署在Tomcat中,然后启动Tomcat,访问地址http://localhost:8080/Struts2/showProduct就能看到效果

提交数据到Action

创建addProduct.jsp

<html>
<form action="addProduct">
 <input type="text" name="product.name">
    <br/>
 <input type="submit" value="submit">
</form>
</html>

修改struts.xml

		<action name="addProduct" class="controller.ProductAction"
			method="add">
			<result name="show">show.jsp</result>
		</action>

ProductAction增加add方法

    public String add(){
        System.out.println("product.name:"+product.getName());
        return "show";
    }

部署在Tomcat中,然后启动Tomcat,访问地址http://localhost:8080/Struts2/addProduct.jsp并提交数据

上传文件到服务器

创建upload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>文件上传</title>
</head>
<body>
	<form action="upload" method="post" enctype="multipart/form-data">
上传文件 : <input type="file" name="doc" /> <br> 
		<input type="submit" value="上传">
	</form>
</body>
</html>

创建UploadAction类

因为视图中定义name字段为“doc”,所以这里的属性只能基于“doc”

package controller;

import java.io.File;

public class UploadAction {

	private File doc;
	private String docFileName;
	private String docContentType;

	public File getDoc() {
		return doc;
	}

	public void setDoc(File doc) {
		this.doc = doc;
	}

	public String getDocFileName() {
		return docFileName;
	}

	public void setDocFileName(String docFileName) {
		this.docFileName = docFileName;
	}

	public String getDocContentType() {
		return docContentType;
	}

	public void setDocContentType(String docContentType) {
		this.docContentType = docContentType;
	}

	public String upload() {
		System.out.println(doc);
		System.out.println(docFileName);
		System.out.println(docContentType);
		return "success";
	}

}

修改struts.xml

添加upload路径,并修改上传文件允许最大值(默认2M)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
	<!-- 设置上传文件最大值,默认为2M -->
	<constant name="struts.multipart.maxSize" value="10240000"></constant>

	<!-- 使用默认的一套拦截器 -->
	<package name="basicstruts" extends="struts-default">
	
		<action name="upload" class="controller.UploadAction" method="upload">
			<result name="success">success.jsp</result>
		</action>

	</package>
</struts>

创建success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" isELIgnored="false"%>
<!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>文件上传成功</title>
</head>
<body>
	uploaded success
	<br />${doc}
	<br /> ${docFileName}
	<br /> ${docContentType}
	<br />
</body>
</html>
部署在Tomcat中,然后启动Tomcat,访问地址 http://localhost:8080/Struts2/upload.jsp并上传文件

猜你喜欢

转载自blog.csdn.net/the_programlife/article/details/80645647