一、struts2的执行流程
二、struts2的版本区别
1.以下环境 jre1.10 ,Tomcat8.5 ,struts2 2.3 会出错,如下:(解决办法升级struts2)
---------------------------------------------------------------------------------
Type Exception Report
Message Illegal char <:> at index 3: jar:file:\E:\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp4\wtpwebapps\UpDownloadFile_Struts2\WEB-INF\lib\struts2-core-2.3.34.jar
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
java.nio.file.InvalidPathException: Illegal char <:> at index 3: jar:file:\E:\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp4\wtpwebapps\UpDownloadFile_Struts2\WEB-INF\lib\struts2-core-2.3.34.jar
java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:229)
java.base/java.io.File.toPath(File.java:2300)
java.base/java.util.zip.ZipFile$Source.get(ZipFile.java:951)
java.base/java.util.zip.ZipFile.<init>(ZipFile.java:216)
java.base/java.util.zip.ZipFile.<init>(ZipFile.java:148)
java.base/java.util.jar.JarFile.<init>(JarFile.java:324)
java.base/java.util.jar.JarFile.<init>(JarFile.java:295)
java.base/java.util.jar.JarFile.<init>(JarFile.java:234)
com.opensymphony.xwork2.util.fs.JarEntryRevision.needsReloading(JarEntryRevision.java:76)
com.opensymphony.xwork2.util.fs.DefaultFileManager.fileNeedsReloading(DefaultFileManager.java:66)
com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.needsReload(XmlConfigurationProvider.java:397)
org.apache.struts2.config.StrutsXmlConfigurationProvider.needsReload(StrutsXmlConfigurationProvider.java:169)
com.opensymphony.xwork2.config.ConfigurationManager.needReloadContainerProviders(ConfigurationManager.java:215)
com.opensymphony.xwork2.config.ConfigurationManager.conditionalReload(ConfigurationManager.java:179)
com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:73)
org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:978)
org.apache.struts2.dispatcher.ng.PrepareOperations.createActionContext(PrepareOperations.java:81)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:89)
三、升级Struts2的版本,下载Struts2 2.5
四、导入Struts2的jar包
五、struts2的六大配置文件
struts2框架按照如下顺序加载以下六大配置文件:
(1)default.properties文件:该文件在核心包struts2-core-2.5.18.jar,org.apache.struts2包里面:default.properties
该文件是设置struts的默认常量配置,可以在自定义的struts.xml文件中进行覆盖以上的配置。如:
<!-- 1.struts2默认常量修改,不能直接修改default.propertis中的常量 -->
<constant name="struts.action.extension" value="action,do,neld,"></constant>
<constant name="struts.serve.static.browserCache" value="false"></constant>
<constant name="struts.devMode" value="true"></constant>
(2)struts-default.xml 文件:该文件在核心包struts2-core-2.5.18.jar下面,该文件包含了struts2框架依赖的对象配置,结果类型和拦截器等。
<package name="struts-default" abstract="true" strict-method-invocation="true">
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.result.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.result.HttpHeaderResult"/>
...
</result-types>
<interceptors>
<interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
<interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
<interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>
...
</interceptors>
(3) struts-plugin.xml 该文件保存在struts2框架的插件中:struts-xxx-plugin-2.3.5.jar,是由插件提供的。
-----------------------------以上三个文件是框架自带的,不修改,只使用-------------------------------
(4)struts.xml 该文件是web应用默认的struts配置文件, 用于配置自定义的常量、package、action、result等
(5)struts.properties 该文件是struts的默认常量属性配置文件,可以修改default.properties中的常量配置
(6)web.xml 该文件是web应用的配置文件,此处通过filter的方式,配置struts2的前端控制器
六、配置web.xml和struts.xml文件
<!-- web.xml文件 -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">
<!-- 前端控制器 :struts2 2.3与2.5区别是没有".ng"-->
<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>
<!--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>
<!-- Add packages here -->
<package name="hellopkg" namespace="/" extends="struts-default">
<action name="hello" class="org.openbox.hello.HelloWorld"
method="execute">
<result name="hello">
<param name="location">/WEB-INF/views/hello.jsp</param>
</result>
</action>
</package>
</struts>
七、Struts2写的helloworld示例
1.action文件HelloWorld.java代码
package org.openbox.hello;
public class HelloWorld {
public String execute() {
System.out.println("hello struts....");
return "hello";
}
}
2.跳转jsp文件hello.jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>hello struts2...</h2>
</body>
</html>
3.浏览器访问结果