java - 无法运行 Struts 2 Hello World
问题描述
问题当我运行我的项目并尝试运行时
ERROR Dispatcher Dispatcher initialization failed
Unable to load configuration. - bean - jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%208.5/wtpwebapps/Struts2Test/WEB-INF/lib/struts2-gxp-plugin-2.5.22.jar!/struts-plugin.xml:27:162
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:69)
and more...
http://localhost:8081/Struts2Test/testAction
这没用。它显示 HTTP 状态 404(在我的浏览器上)
Eclipse 控制台
Eclipse 控制台上没有错误
/Struts2Test/src/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="test" extends="struts-default">
<action name="testAction" class="test.Action.TestAction" method="execute">
<result name="success">
/success.jsp
</result>
<result name="error">
/error.jsp
</result>
</action>
</package>
</struts>
测试动作.java
package test.Action;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport
{
public String execute()
{
return "success";
}
}
/Struts2Test/WebContent/WEB-INF/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>Struts2Test</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>
解决方案
你好世界(在struts中)
在 Eclipse 企业版中创建动态 Web 项目
Eclipse>文件>新建>动态Web项目
给它起个名字:HelloWorld
它应该在 Web 内容文件夹>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>HelloWorld</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>
创建一个类 TestAction.java
(/HelloWorld/src/com/test/TestAction.java)
package com.test;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport
{
public String execute()
{
return "success";
}
}
/HelloWorld/WebContent/success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1> Hello World</h1>
</body>
</html>
/HelloWorld/src/struts.xml
注意:您的 struts.xml 文件应位于动态 Web 项目的 src 文件夹中。否则它将无法正常工作。
<?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="test" extends="struts-default">
<action name="testAction" class="com.test.TestAction" method="execute">
<result name="success">
/success.jsp
</result>
<result name="error">
/error.jsp
</result>
</action>
</package>
</struts>
您需要在 Java 构建路径中添加这些 jar 文件
选择您的项目>右键单击>属性>Java构建路径>添加外部Jar文件
现在,您需要在部署程序集中添加这些 jar 文件
选择您的项目>右键单击>属性>部署程序集>添加>单击 Java 构建路径>您之前添加的 jar 文件将出现在那里。选择并确定。
在服务器上运行您的项目
选择您的项目>运行方式>在服务器上运行
在您的浏览器上
http://localhost:8081/HelloWorld/testAction
(我将端口号从 8080 更改为 8081。Tomcat 的默认端口号是 8080。因此 URL 将为http://localhost:8080/HelloWorld/testAction。)
推荐阅读
- c++ - C++ find_if 导致绑定函数崩溃
- postgresql - 为什么 psql 中没有添加此列?
- java - Java如何将对象作为参数从类传递给另一个类
- c# - 如何在 startup.cs 中添加提供程序而不覆盖所有预先存在的提供程序?
- excel - How to add folder
- regex - matching first occurrence of a symbol without anything else in EditPad
- python - python-如何正确选择k个最佳数值特征?
- python - Sparse MATRIX - Error TypeError: 'str' object cannot be interpreted as an integer
- python - Python: Define function with name from variable string
- python - how can I extract courses title/school/description from a website (such as Udacity)