Недавно я установил образец проекта, в котором я определил действие, но по-прежнему не получаю ошибки действия. Как вы увидите в struts2.xml, действие с именем login уже определено.
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>StrutsLoginDemoApp</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/action/*</url-pattern>
</filter-mapping>
<!-- session config -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
<!-- for restricting direct access of jsp pages -->
<!-- <security-constraint>
<web-resource-collection>
<web-resource-name>block_jsp_pages</web-resource-name>
<url-pattern>*.jsp</url-pattern>
</web-resource-collection>
<auth-constraint />
</security-constraint> -->
</web-app>
struts2.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>
<!-- removing action extension -->
<constant name = "struts.action.extension" value = "" />
<!-- enabling developer mode -->
<constant name = "struts.devMode" value = "true" />
<package name = "default" extends = "struts-default"
namespace = "/action">
<!-- declarartion of interceptor -->
<interceptors>
<interceptor name = "authenticationInterceptor"
class = "com.demo.interceptor.AuthenticationInterceptor">
</interceptor>
<interceptor-stack name = "secureStack">
<interceptor-ref name = "authenticationInterceptor" />
<interceptor-ref name = "defaultStack" />
</interceptor-stack>
</interceptors>
<action name = "login">
<result>/login.jsp</result>
</action>
<action name = "loginAction" class = "com.demo.action.UserLoginAction">
<result name = "success" type = "redirectAction">
<param name = "actionName">task</param>
<param name = "namespace">/action</param>
</result>
<result name = "login">/login.jsp</result>
</action>
<action name = "logout" class = "com.demo.action.UserLogoutAction">
<result name = "success" type = "redirectAction">
<param name = "actionName">login</param>
<param name = "namespace">/action</param>
</result>
</action>
<action name = "task">
<interceptor-ref name = "secureStack" />
<result>/task.jsp</result>
<result name = "login">/login.jsp</result>
</action>
</package>
</struts>
и index.jsp, который просто вызывает действие, указанное выше.
<%@ page language = "java" contentType = "text/html; charset=ISO-8859-1"
pageEncoding = "ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Welcome to the Struts2 App</h1><br>
<a href = "action/login">Login</a>
</body>
</html>
Кто-нибудь может объяснить, что здесь не так?
Конфигурация маршрутизации Struts2 находится в файле с именем struts.xml
, а не struts2.xml
, возможно, это ваша проблема.