Error 404 on using servlets with struts 2

2019-08-13 01:36发布

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条回答
Anthone
2楼-- · 2019-08-13 02:02

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>
查看更多
登录 后发表回答