Я впервые делаю свое простое приложение с 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
Даже я получаю ошибку 404 при запуске этого URL: 8080/StrutsFirstApplication/run.action
почему и как это исправить?
Вы не указали свой 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, попробуйте использовать это:
<?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>
Проблема в вашем файле конфигурации проверяется ниже и соответствует:
<!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.
Это было какое-то время, когда я работал над struts, но я думаю, что возвращение действия должно соответствовать строке в xml configuraion
Я имею в виду
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>
Сделайте строку возврата одинаковой с обоих концов, я надеюсь, что это может сработать.