Error 404 on using servlets with struts 2

China☆狼群 提交于 2019-12-24 16:10:31

问题


I have all the Struts jar included in WEB-INF/lib and imported to project. I am trying to migrate from simple servlets based project to Struts2. I added filter tag in web.xml and constant tag in struts.xml as per the tutorial , yet I get 404 on servlet invocation.

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>

error on JSP page :

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:


There should be a pattern

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

and servlet mapping should be

<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>


来源:https://stackoverflow.com/questions/21305186/error-404-on-using-servlets-with-struts-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!