使用带有struts 2的servlet时出错404

时间:2014-01-23 10:17:00

标签: java jsp servlets struts2 http-status-code-404

我将所有Struts jar都包含在WEB-INF/lib中并导入到项目中。我正在尝试从简单的基于servlet的项目迁移到Struts2。我按照教程在web.xml中添加了过滤器标记,在struts.xml中添加了常量标记,但我在servlet调用上获得了404。

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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Solution</display-name>
  <welcome-file-list>
    <welcome-file>Login.jsp</welcome-file>
  </welcome-file-list>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
    <display-name>LoginController</display-name>
    <servlet-name>LoginController</servlet-name>
    <servlet-class>com.tcs.controller.LoginController</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginController</servlet-name>
    <url-pattern>/LoginController</url-pattern>
  </servlet-mapping>
</web-app>

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.action.excludePattern" value="/LoginController"></constant>
    <package name="default" extends="hibernate-default">    
    </package>
</struts>
JSP页面上的

错误:

HTTP Status 404 - There is no Action mapped for namespace / and action name LoginController.
The requested resource (There is no Action mapped for namespace / and action name LoginController.) is not available.

1 个答案:

答案 0 :(得分:1)

应该有一种模式

<constant name="struts.action.excludePattern" value="/LoginController/?.*"/>

和servlet映射应该是

<servlet>
  <servlet-name>LoginController</servlet-name>
  <servlet-class>com.tcs.controller.LoginController</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>LoginController</servlet-name>
  <url-pattern>/LoginController/*</url-pattern>
</servlet-mapping>