1. 程式人生 > >Struts2基本環境搭建過程中容易出現的丟包問題

Struts2基本環境搭建過程中容易出現的丟包問題

就拿一個最簡單的struts2的hello world為例來說明吧。

        struts2是繼承自ActionSupport類,所以說一個最簡單的hello world是不能缺少該類的包的,也就是說不能缺少xwork-core-2.3.16.jar,由於我們要搭建的框架是struts2,所以說也不能缺少struts2-core-2.3.16.jar,否則這就不是struts2框架了。

綜上所述,一個struts2框架必不可少的包檔案有:

struts2-core-2.3.16.jar
xwork-core-2.3.16.jar

struts2框架的HelloWorld原始碼如下:

Struts2Test.java原始碼:

package com.test;

import com.opensymphony.xwork2.ActionSupport;

public class Struts2Test extends ActionSupport{

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

}

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>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
	<action name="hello" class="com.test.Struts2Test">
		<result name="success">/success.jsp</result>
	</action>
</package> 
</struts>    

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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	</filter-class>
  </filter> 
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

index.jsp原始碼:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  <body>
    This is my JSP page. <br>
  </body>
</html>

success.jsp原始碼:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
    <base href="<%=basePath%>">
    <title>SUCCESS</title>
  </head>
  <body>
    SUCCESS! <br>
  </body>
</html>

執行程式:

當出現下面的錯誤時:

嚴重: Exception starting filter struts2
java.lang.NoClassDefFoundError: ognl/PropertyAccessor

根據提示,這說明此時缺少的包檔案是ognl,所以說我們要匯入該包:

ognl-3.0.6.jar

重啟,繼續執行程式

當出現下面的錯誤時:

嚴重: Dispatcher initialization failed
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

根據提示,此時出現的錯誤是“排程程式初始化失敗”,而實現該功能的包是javassist,所以說匯入該包:

javassist-3.11.0.GA.jar

繼續:

此時的錯誤提示為:

嚴重: Exception starting filter struts2
java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils

        根據提示,此時出現的問題是沒有找到類,而該類是StringUtils,所以說我們要匯入該類所在的包檔案,由於提示中說該類在lang3包中,該包的上一級包是commons,所以根據提示,我們很容易找到下面的包,直接匯入即可。

commons-lang3-3.1.jar

繼續執行:

此時報的錯誤為:

警告: Could not create JarEntryRevision for [jar:file:/C:/apache-tomcat-6.0.20/webapps/Struts2Test/WEB-INF/lib/struts2-core-2.3.16.jar]!
java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils

根據提示,我們需要匯入的包檔案為:commons/io,也就是說,我們需要匯入的包檔案為commons-io,所以說此處匯入的包檔案為:

commons-io-2.2.jar

繼續執行:

根據提示,此時報的錯誤是:

嚴重: Dispatcher initialization failed
Unable to load configuration. - bean - jar:file:/C:/apache-tomcat-6.0.20/webapps/Struts2Test/WEB-INF/lib/struts2-core-2.3.16.jar!/struts-default.xml:64:179

        根據提示,此時出現的問題仍然是前面出現的“排程程式初始化失敗”,此時我們應該看其第二條資訊,第一次出現“排程程式初始化失敗”時,第二行報的錯誤是:“InvocationTargetException”,而現在再出現“排程程式初始化失敗”時,第二行的報錯資訊為:“struts-default.xml:64:179”,所以說此時報的錯誤並不是上次的同一個錯誤,此時需要匯入的包檔案為:

commons-fileupload-1.3.jar

繼續執行:

此時報的錯誤資訊為:

嚴重: Dispatcher initialization failed
Unable to load configuration. - bean - jar:file:/C:/apache-tomcat-6.0.20/webapps/Struts2Test/WEB-INF/lib/struts2-core-2.3.16.jar!/struts-default.xml:69:87

        額。。。貌似剛才的問題沒有被解決,但是仔細看第二行提示,此時的提示為:struts-default.xml:69:87,也就是說報的錯誤資訊和剛才的其實是不一樣的,也就是說這不是上一個問題,上一個問題已經被解決了,而這是一個新問題,此時匯入的包檔案為:

freemarker-2.3.19.jar

繼續執行程式:

此時提示的資訊是:資訊: 

Server startup in 7228 ms,

也就是說此時程式執行已經沒有問題了。

接下來就是訪問該程式的路徑了:

http://localhost:8080/Struts2Test/

執行的結果:


當訪問路徑為:

http://localhost:8080/Struts2Test/hello

此時執行的結果為: