javaweb action无法跳转、表单无法跳转的解决方法

action无法跳转,表单无法跳转的解决方法

刚在网上搜索了一下,发现我的这篇文章已被非常多人转载了去其他站点。暗爽大笑,只是还是希望大家注明出处。

顺便说明一下。下面是在struts2中通过測试的


action无法跳转、表单无法跳转的原因:
说明 :在确保你项目的其他action、表单能正常跳转的情况下,查找下面3个原因。

1、确认action中有无写错,struts中有无指定action的class
如:<action name="*User" class="com.login.action.LoginAction" method="{1}">,假设后台java中没有这里class指定的路径,那么将无法跳转

2、表单有没有后缀,如指定后缀是action,则一定要配置  <url-pattern>*.action</url-pattern>或<url-pattern>/*</url-pattern>

3、form表单中的action是否可以与struts.xml里action的name匹配
如:jsp表单<form action="saveUser.action" id="infoForm" method="post">,假设在struts.xml找不到<action name="*User" class=" ####  ">(这里的*User,*是通配符)或<action name="saveUser" class=" ####  ">,将无法跳转


jsp form表单中的action无法跳转
假设表单中action中没有后缀,使用   <url-pattern>*.action</url-pattern> 这样的配置将无法跳转,由于匹配不了后缀名。

 使用 <url-pattern>*.action</url-pattern> 时
如:
1、
<form action="saveChild.action" id="infoForm" method="post">  action有后缀名,这样的能正常跳转

2、<form action="saveChild" id="infoForm" method="post">  action没有后缀名,无法跳转

但假设使用   <url-pattern>/*</url-pattern> 在表单中即使action中没有后缀名,即<form action="saveChild" id="infoForm" method="post">。也能跳转

假设表单正常跳转,控制台应该会打印出这种方法。如:
2014-05-06 12:11:52,404 DEBUG (org.apache.struts2.interceptor.FileUploadInterceptor:68) - Bypassing //saveChild
2014-05-06 12:11:52,407 DEBUG (org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor:68) - Validating //saveChild with method save.

依据我自己的经验,假设控制台没有打印出“Bypassing // action名”,那么表单的action都是无法正常跳转到后台的

 <url-pattern>/*</url-pattern>

使用 <url-pattern>/*</url-pattern>这样的会将servlet拦截了,假设项目中有单独创建的servlet。那么将无法訪问这个servlet,如平时使用servlet创建一个验证码的链接

所以假设要使用验证码的servlet,那么就将使用下面这样的配置了

 <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>*.action</url-pattern>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>

jsp訪问时,无法訪问
假设仅仅定义了   <url-pattern>*.action</url-pattern>;而未定义 <url-pattern>*.jsp</url-pattern>。那么jsp也将无法訪问


一个通过測试的web.xml文件。没有给出web.xml文件头,由于包括了url,博客不准发url

  <display-name>unionweb</display-name>
   
  <!-- 监听器,整合spring中使用 -->
   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
  <!-- spring的 applicationContext.xml文件存放路径,类路径-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
 
  <!-- 下面是struts2的过滤器的配置 -->
  <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>/*</url-pattern>
       -->
    <url-pattern>*.action</url-pattern>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
 

说明:以上所有都是我自己经过測试的,如说的不正确。还望大家指出 



原文地址:https://www.cnblogs.com/ldxsuanfa/p/10008444.html