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-pat tern> 时
如:
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>
 

说明:以上全部都是我自己经过测试的,如说的不对,还望大家指出 



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值