一、前提须知
Struts2 框架的入门学习,到底需要哪些东西?以下列举之
- JDK:1.8.0_65
- IDE:Java EE
- 插件:struts-2.5.2 以及 apache-tomcat-8.0.37
二、下载,解压
JDK,JavaEE之类的,下载不再赘述,下面我们主要介绍struts2 以及 tomcat的下载及版本的选择
1 Struts 2.5.2,博主选择的是Struts 2.5.2版本
2 Tomcat 8.0.37,博主选择的是Tomcat 8.0.37
3 右键解压,不再赘述
三、新建Web 项目工程
1 新建项目工程
2 新建HelloWorldAction类
HelloWorldStruts2/Java Resources/src 路径下,新建包 com.struts,包下新建类 HelloWorldAction.java
HelloWorldAction.java代码如下:
public class HelloWorldAction {
private String name;
public String execute() throws Exception {
if ( getName().equals("") || getName() == null ){
return "error";
}
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
四、文件配置
1 导入jar包
需要从刚刚解压的stuts-2.5.2的lib目录下,复制所需要的jar包,粘贴到 HelloWorldStruts2/WebContent/WEB-INF/lib 目录下,如图
2 struts.xml配置
HelloWorldStruts2/Java Resources/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是任意的,只要不重复即可 -->
<package name="com.struts" extends="struts-default" namespace="/">
<action name="hello" class="com.struts.HelloWorldAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
3 HelloWorldStruts2/WebContent 路径下,新建index.jsp, success.jsp, error.jsp
index.jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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>
<title>Hello World</title>
</head>
<body>
<h1>Hello World Struts2</h1>
<!-- form action值一定要和struts.xml中action name值匹配 -->
<form action="hello">
<label for="name">Please enter your name</label><br/>
<input type="text" name="name"/>
<input type="submit" value="Enter"/>
</form>
</body>
</html>
success.jsp 代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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>Hello World</title>
</head>
<body>
Hello World, Welcome! <s:property value="name"/>
</body>
</html>
error.jsp 代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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>Hello World</title>
</head>
<body>
You did not have entered a name!
</body>
</html>
4 最后,我们需要更改 HelloWorldStruts2/WebContent/WEB-INF/ 路径下的 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>HelloWorldStruts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
五、运行
右键HelloWorldStruts2 项目,然后 Run As -> Run on Server,点击finish运行,出现下图:
输入姓名:World_Programming,点击Enter
什么也不做,直接Enter
六、Struts2的执行流程
- 浏览器地址栏中输入:http://localhost:8080/HelloWorldStruts2/ 或者http://localhost:8080/HelloWorldStruts2/index.jsp,因为在web.xml中的< welcom-file-list>标签中,存在一个 < welcome-file>index.jsp< /welcome-file>标签,如果地址栏中的项目名称后没有填写其他的jsp文件,框架会帮我自动加载默认的index.jsp
- 服务器根据index.jsp中的form action值,去struts.xml中寻找对应的action,再根据找到的action,找到对应的class类,然后通过反射,执行该类中的execute方法,再根据方法的返回值,再去struts.xml中匹配对应的result name值,实现页面的跳转。
七、问题综述
java.lang.ClassNotFoundException:org.apache.struts2.dispatcher.FilterDispathcher
原因:struts 2.1.3版本之前,web.xml配置:
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
struts 2.5.2版本,web.xml配置:
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
我们也可以从 struts2-core-2.5.2.jar 目录下看到,FilterDispatcher.class 不存在了,代替的是 StrutsPrepareAndExecuteFilter.class