我是第一次使用Struts2进行简单的应用程序。
这是我的代码,
ActionExample:
package com.demo;
import com.opensymphony.xwork2.Action;
public class ActionExample implements Action
{
@Override
public String execute() {
return SUCCESS;
}
}
success.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>Insert title here</title>
</head>
<body>
Success Page!.........
</body>
</html>
struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="run" class="com.demo.ActionExample">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>StrutsFirstApplication</display-name>
<welcome-file-list>
<welcome-file>success.jsp</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>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
我正在通过此视频链接https://www.youtube.com/watch?v=ATrmHSfh6Tc
进行操作即使我在运行此url时遇到404错误:8080 / StrutsFirstApplication / run.action
为什么以及如何解决这个问题?
答案 0 :(得分:0)
您没有发布index.jsp。它应该是这样的:
<%@ taglib prefix="s" uri="/struts-tags"%>
...
...
<form action="run">
<s:submit/>
</form>
你的struts.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="run" class="default.ActionExapmle " method="execute">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
现在,我不知道在web.xml文件中使用所有welcome文件是否正确,请尝试使用:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
答案 1 :(得分:0)
配置文件中的问题请检查以下内容并匹配:
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<action-mappings>
<<action name="run" class="com.demo.ActionExample">
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
</struts-config>
相应地改变。我建议您切换到struts2,因为struts1是EOL。
答案 2 :(得分:-1)
我已经花了一段时间研究struts,但我认为动作的返回应该与xml配置中的字符串相匹配
我的意思是
public String execute() {
return SUCCESS; // returns SUCCESS and not success
}
你的xml有
<action name="run" class="com.demo.ActionExample">
<result name="success">/success.jsp</result>
</action>
使两端的返回字符串相同,我希望这可能有用。