0

我将代码更改为使用 Jquery 表单插件,以便上传图片。我原来的形式很简单

$.ajax({
    type: "POST",
    url: "register.htm",
    data:{
            account_name: $('#regAccount_name').val(),
            pwd: MD5($('#regPwd').val()),
            fname: $('#regFname').val(),
            lname: $('#regLname').val(),
    },
    success: function(data){
        changeMainIFrame('MenuFrame.jsp?regStatus=User Successfully Registered');
    },
    error:function(e){
        console.log("sendRegistration(): " + e);
    }

});

但改成这个

    var options = { 
         beforeSend: function() 
         {
             //alert(MD5($('#regPwd').val()));
            // document.getElementById('regPwd').value = MD5($('#regPwd').val());

         },
         uploadProgress: function(event, position, total, percentComplete) 
         {

         },
         success: function() 
         {
             changeMainIFrame('MenuFrame.jsp?regStatus=User Successfully Registered');
         },
         complete: function(response) 
         {
         },
         error: function(e)
         {
             console.log("sendRegistration(): " + e);

         }

     }; 

          $("#regForm").ajaxForm(options);

但是现在我收到一个 400 Bad Request,我认为这意味着我的控制器甚至没有收到请求。这是我的控制器代码:

    @RequestMapping("/register.htm")
public String register(//   @RequestParam("regProfilePic") File pic,
                        @RequestParam("regAccount_name") String account_name,
                        @RequestParam("pwd") String pwd,
                        @RequestParam("fname") String fname,
                        @RequestParam("lname") String lname,

                       ModelMap params){

    setup();

    System.out.println("Register");
    service.registerUser(fname, lname, account_name, pwd);

    return "MenuFrame";
}

这是我的 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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Volt</display-name>

  <welcome-file-list>
    <welcome-file>Mugenjou.jsp</welcome-file>
  </welcome-file-list>

    <session-config>
      <session-timeout>30</session-timeout> 
    </session-config>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/beans.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>volts</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>volts</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <jsp-config>
        <taglib>
            <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
            <taglib-location>/WEB-INF/tld/c.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri>
            <taglib-location>/WEB-INF/tld/fmt.tld</taglib-location>
        </taglib>
    </jsp-config>
</web-app>

这是我的 servlet

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.mugenjou.control" />

    <!--  /WEB-INF/jsp/VIEWNAME.jsp --> 
    <bean id="viewNameTranslator" class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator"/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <!-- <property name="prefix" value="/WEB-INF/jsp/"/> -->
         <property value="" name="prefix"/> <property value=".jsp" name="suffix"/>
    </bean>


</beans>

html

<form id ='regForm' action="register.htm" method="POST" enctype="multipart/form-data">

                <table cellpadding=5>

                    <!-- <tr>
                        <td>Profile Picture <br> <input type = "file" id ="regProfilePic" name ="regProfilePic"  /></td>
                    </tr> -->

                    <tr>
                        <td> *Account Name <br> <input id ="regAccount_name" name = "regAccount_name" type="text" size ="20" onblur="isAccount_name();registerBtn();"/></td>

                        <td>
                            <div id ="chkUserLb"></div>
                        </td>
                    </tr>

                    <tr>
                        <td> *Password <br> <input id = "regPwd" name = "pwd" type="text" size ="20" onblur="isPwd();registerBtn();"/></td>
                        <td id=chkPwdLb></td>
                    </tr>

                    <tr>
                        <td> *First Name <br> <input id = "regFname" name ="fname" type="text" size ="20" onblur="isFname();registerBtn();"/></td>
                        <td id=chkFnameLb></td>
                    </tr>

                    <tr>
                        <td> *Last Name <br> <input id = "regLname" name = "lname" type="text" size ="20" onblur="isLname();registerBtn();"/></td>
                        <td id=chkLnameLb></td>
                </tr>


                <tr>
                    <td><input id = "regSubmitBtn" type="submit" value="Submit" disabled></td>
                </tr>

            </table>

            *Required

          </form>
4

1 回答 1

0

发现了问题,我需要使用 @RequestParam("regProfilePic") Multipart File pic 而不是 File pic

于 2013-09-30T17:02:54.557 回答