单击按钮后,表单未提交

时间:2014-04-08 09:58:58

标签: spring spring-mvc

请检查以下代码:

更新的web.xml是:

<display-name>Spring MVC Form Handling</display-name>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
      </servlet-mapping>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  </web-app>
这就是为什么UI搞砸了。   链接: http://postimg.org/image/cf80ko6in/ 和spring-servlet.xml是:

<context:component-scan base-package="com.hrportal.controller" />
<bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en" />
    </bean>

    <bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
        </property>
            </bean>

I do not know how to handle this warnings for error:404:

服务器日志,我收到错误&#39; 404&#39;在浏览器的控制台上..

12:42:40,416 DEBUG DispatcherServlet:766 - DispatcherServlet with name 'spring' processing GET request for [/HRportal/scripts/jquery-1.9.1.js]
12:42:40,417  WARN PageNotFound:1020 - No mapping found for HTTP request with URI [/HRportal/scripts/jquery-1.9.1.js] in DispatcherServlet with name 'spring'
12:42:40,418 DEBUG DispatcherServlet:881 - Successfully completed request
12:42:40,419 DEBUG DispatcherServlet:766 - DispatcherServlet with name 'spring' processing GET request for [/HRportal/images/dataZen_Logo.png]
12:42:40,420  WARN PageNotFound:1020 - No mapping found for HTTP request with URI [/HRportal/images/dataZen_Logo.png] in DispatcherServlet with name 'spring'
12:42:40,421 DEBUG DispatcherServlet:881 - Successfully completed request
12:42:40,497 DEBUG DispatcherServlet:766 - DispatcherServlet with name 'spring' processing GET request for [/HRportal/images/facebook-icon.png]
12:42:40,499  WARN PageNotFound:1020 - No mapping found for HTTP request with URI [/HRportal/images/facebook-icon.png] in DispatcherServlet with name 'spring'
12:42:40,499 DEBUG DispatcherServlet:881 - Successfully completed request
12:42:40,501 DEBUG DispatcherServlet:766 - DispatcherServlet with name 'spring' processing GET request for [/HRportal/images/imagestwitter.png]
12:42:40,503  WARN PageNotFound:1020 - No mapping found for HTTP request with URI [/HRportal/images/imagestwitter.png] in DispatcherServlet with name 'spring'
12:42:40,527 DEBUG DispatcherServlet:881 - Successfully completed request

我无法解决这些警告。

1 个答案:

答案 0 :(得分:0)

你犯了一些错误:

1)您的表单操作为upcomingLeaves.do

<form:form commandName="loginForm" id="loginForm" action="upcomingLeaves.do">

并在Controller中有/upcomingLeaves

@RequestMapping(value = "/upcomingLeaves" ,method = RequestMethod.POST)

2)您尝试获取表单但已使用按钮初始化。

$("button").click(function(){
         var $form = $(this); ....

3)我认为你想提交一个表格,其中包含一个即将离开的表格,但你有所有即将到来的单一形式的表格。因此,当您提交表单时,Jquery将序列化所有即将到来的Leaves。

4)在您的情况下,您应该执行以下操作:AcceptRejectCancel,然后不需要$form.serialize(),您可以通过发送员工ID本身来执行这些操作。

<小时/> 的解决方案: 使用员工ID渲染锚标签。并使工作更简单..

像:

<a class="emp-leave-action"
   href='<c:url value="/leaveaccept.do/${upComLeave.employee_id}"/>'>Accept</a>
<a class="emp-leave-action"
   href='<c:url value="/leavereject.do/${upComLeave.employee_id}"/>'>Reject</a>
<a class="emp-leave-action"
   href='<c:url value="/leavecancel.do/${upComLeave.employee_id}"/>'>Cancel</a>

并实现GET方法,如: 接受:

@RequestMapping(value = "/leaveaccept.do/{empId}" ,method = RequestMethod.GET)
public String processAccept(@PathVariable("empId") Integer empId) {

    //do emplyee leave accept work here
}

拒绝:

@RequestMapping(value = "/leavereject.do/{empId}" ,method = RequestMethod.GET)
public String processReject(@PathVariable("empId") Integer empId) {

    //do emplyee leave reject work here
}

取消:

@RequestMapping(value = "/leavecancel.do/{empId}" ,method = RequestMethod.GET)
public String processCancel(@PathVariable("empId") Integer empId) {

    //do emplyee leave cancel work here
}

最后使用AJAX调用:

$("a.emp-leave-action").click(function(e){
   e.preventDefault();
   $.get( $(this).attr('href'), function( data ) {      
      alert( "action was performed." );
    });
});