[Struts2 框架学习] 一、Hello,Struts
前言
SSH 框架仍然是常用的 MVC 框架,故仍需好好学习一下。在公司里头,使用的是 Spring + Struts2 封装后的框架,环境之类的也配置好了,故每次开发的时候,确实是很方便,可是一旦脱离那个环境则不晓得该如何是好。故打算自己一步步学习。Spring 之前已经有所了解,故当前的计划是先学习 Struts2 框架。
学习的素材:以马士兵老师讲解的 Struts2 视频为主(Struts 2.1 版本,虽然马老师的视频比较久远,不过 2.1 仍然不算太远,现在最新的版本为 2.5.13),官方材料为辅,以及借助于搜索进行扩展。
环境介绍
- 系统:MacOS Sierra 10.12.6
- JDK:1.8.0_112
- 开发工具:Eclipse 4.7.1a
- Tomcat 版本:9.0
- Struts2 版本:2.5.13(官网地址:http://struts.apache.org/)
第一个 Struts2 项目 – Hello,Struts
在 Eclipse 中新建 Web 项目
导入 Struts2 必要的 jar 包(在这一步里头我走了一些弯路,一开始我偷懒的选择了把全部 jar 包导入,结果运行报错。之后搜索结果告诉我没必要导入全部的 jar 包,导入必要的文件即可。之后,则根据一些教程上导入的 jar 包导入,不过由于 Struts 2 版本的不同,需要导入的 jar 包有一点的不同,例如 Struts2 2.5 版本开始 xwork-core-x.x.x.jar 包就包含在了 struts2-core-x.x.x.jar 包之中了。在一次次看错误信息之后,才导入了必要的 jar 包。这里推荐,直接上 Struts2 官网的下载链接之中,就有必要 jar 包的下载,下载有标注为:Essential Dependencies Only 的压缩文件即可。毕竟每个版本所导入的必须 jar 包可能有所不同)。
配置 web.xml 和 struts.xml
新建 JSP 文件
运行
导入 jar 包
Struts2 2.5.13 的必须 jar 包如下(这里以 2.5.13 版本为例,下同):
commons-fileupload-1.3.3.jar
commons-io-2.5.jar
commons-lang3-3.6.jar
freemarker-2.3.23.jar
javassist-3.20.0-GA.jar
log4j-api-2.8.2.jar
ognl-3.1.15.jar
struts2-core-2.5.13.jar
配置 web.xml
马士兵老师的建议是,这些配置,没必要自己手把手敲,不仅容易出错,而且也不容易记住,需要用到的时候,直接复制粘贴就可以了,下面的 struts.xml 相同。
配置不同版本可能有所不同,推荐的方式是直接下载 Struts2 官方的范例,从范例之中拷贝出来即可。如果在官网上下载的是 Full Distribution 版本(即 struts-2.5.13-all.zip),则在压缩包下的 apps 目录下。也可以单独下载范例,官网上下载 Example Applications 版本(即 struts-2.5.13-apps.zip)。然后拷贝其中一个范例的 web.xml 中的代码即可。
复制 web.xml 配置 filter 部分,或者整个 web.xml 拷贝进来,下面我则是只复制 filter 部分到原有的 web.xml 文件之中。整个 web.xml 配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>learnStruts2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Filters -->
<!-- START SNIPPET: filter -->
<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- END SNIPPET: filter -->
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
配置 struts.xml 文件
在 src 目录下新建 struts.xml 文件或者直接从官方范例之中拷贝 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="default" namespace="/" extends="struts-default">
<action name="hello">
<result>
/hello.jsp
</result>
</action>
</package>
</struts>
新建 jsp 文件
在 WebContent 下新建 hello.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>Hello,Struts2</title>
</head>
<body>
Hello,Struts2
</body>
</html>
运行
浏览器输入链接:http://localhost:8080/项目名称/hello.action
.action 可以省略
运行结果:
页面显示:Hello,Struts2
OK,最基本的 Hello,Struts 配置与运行成功。
后记
编辑:HochenChong
时间:2017-11-15