即使在xml文件中定义了操作,也不会执行任何操作

时间:2018-05-01 14:51:18

标签: java xml struts2

我最近设置了一个示例项目,其中我定义了操作但仍然没有出现操作错误。正如您将能够在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>

有谁可以解释这里有什么问题?

1 个答案:

答案 0 :(得分:0)

Struts2路由配置位于名为struts.xml而非struts2.xml的文件中,可能是您的问题。