SpringMVC与Ajax& jQuery的

时间:2016-06-17 08:41:22

标签: project-structure

Hi Everybody,

Here I am using
Java - 1.7,
Tomcat Web Server 1.8
Spring 4.x
ajax
Jquery "https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"

My problem is when I am trying send values from ajax to spring controller I am getting one warning called "IOException"

Saying that

"org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver logException
WARNING: Handler execution resulted in exception: Required String parameter 'deptId' is not present"

最初我的数据发送到控制器,但后来也只是首先打印剩余的所有都给出了undefined / null,但我在ajax调用中硬编码值并发送到spring控制器然后它工作正常

And my data is not being reached to Spring controller

**pox.xml**
  =======

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.spring.web</groupId>
    <artifactId>SpringAjaxJquery</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringAjaxJquery Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>4.2.1.RELEASE</spring.version>
        <jdk.version>1.7</jdk.version>
        <hibernate.version>4.3.6.Final</hibernate.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.json</groupId>
            <artifactId>javax.json-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <!-- Hibernate Dependencies -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>SpringAjaxJquery</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

**SpringController.java**
=========================

package com.spring.web.controller;

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.spring.web.model.Spinning_Department;

@Controller
public class SpringController {

    @RequestMapping(value = "/spinning", method = RequestMethod.POST)
    public String spinningDept(
                                @RequestParam("deptName") String deptName,
                                @RequestParam("deptId")String deptId,
                                @RequestParam("yarnCount") String yarnCount,
                                @RequestParam("shiftNo") String shiftNo,
                                @RequestParam("shiftInc") String shiftInc,
                                @RequestParam("deptInc") String deptInc
                                ) throws Exception {
        System.out.println("From spinning dept....");
        Spinning_Department sd = new Spinning_Department();

        sd.setDeptName(deptName);
        sd.setShiftNumber(shiftNo);
        sd.setShiftInc(shiftInc);
        sd.setYarnCount(yarnCount);
        sd.setDeptInc(deptInc);

        System.out.println(sd.getDeptName());
        System.out.println(sd.getYarnCount());
        System.out.println(sd.getShiftNumber());
        System.out.println(sd.getShiftInc());
        System.out.println(sd.getDeptInc());

        return "Data receive successfully";
    }
}


**Spinning_Department**
========================

public class Spinning_Department {

    private String deptName;
    private String deptId;
    private String yarnCount;
    private String shiftNumber;
    private String shiftInc;
    private String deptInc;
//Setters & Getters
}

**spring-servlet.xml**
======================
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <context:component-scan base-package="com.spring.web.controller" />
    <context:component-scan base-package="com.spring.web.model" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <mvc:annotation-driven />

</beans>

**web.xml**
===========

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>


**index.jsp**
=============

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page isELIgnored="false"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<title>Add Users using ajax</title>

</head>
<body>
        <a href="depts.html"> Spinning</a>
</body>
</html>


**depts.jsp**
=============

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page isELIgnored="false"%>
<%@ page session="false"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<!-- <script type="text/javascript" src="./scripts/jquery-2.1.4.js"></script> -->
<html>
<head>
<title>Departments</title>
<script type="text/javascript">
    $(document).ready(
            function() {

                $("#spin").click(
                        function() {

                            $.ajax({
                                type : "POST",
                                url  : "spinning.html",
                                data : 'deptName='+$("#deptName").val()+"&deptId;="+$("#deptId").val()+ "&yarnCount;="+ $("#yarnCount").val()+"&shiftNo;="+$("#shiftNo").val()+"&shiftInc;="+ $("#shiftInc").val()+"&deptInc;="+$("#deptInc").val(),
                                success : function(response) {

                                    $("#msg").html(response);
                                    return false;
                                },
                                error : function() {
                                    $("#msg").html("Error while request....");
                                    return false;
                                }
                            });
                        });
            });
</script>
</head>
<body>

<div>

    <form name="deptForm">
        <span id="msg"></span>

        <table>
            <tr>
                <td><label for="deptName">DeptName<em>*</em></label></td>
                <td><input tabindex="1" size="15" type="text" name="deptName"
                    id="deptName" /></td>
            </tr>
            <tr>
                <td><label for="deptId">DeptId<em>*</em></label></td>
                <td><input tabindex="2" size="5" type="text" id="deptId" name="deptId"/></td>
            </tr>``
            <tr>
                <td><label for="yarnCount">Yarn Count</label></td>
                <td><input tabindex="3" size="5" type="text" id="yarnCount" name="yarnCount"/></td>
            </tr>
            <tr>
                <td><label for="shiftNo">Shift No.</label></td>
                <td><input tabindex="4" size="2" type="text" id="shiftNo" name="shiftNo"/></td>
            </tr>

            <tr>
                <td><label for="shiftInc">Shift Supervisor</label></td>
                <td><input tabindex="5" size="20" type="text" id="shiftInc" name="shiftInc"/></td>
            </tr>

            <tr>
                <td><label for="deptInc">Dept Incharge</label></td>
                <td><input tabindex="6" size="20" type="text" id="deptInc" name="deptInc"/></td>
            </tr>
            <tr>
                <td><input type="submit" value="Submit" id="spin"></td>
            </tr>
        </table>
    </form>
    </div>
</body>
</html>

项目结构

enter image description here

1 个答案:

答案 0 :(得分:0)

我找到了上述解决方案的解决方案 我刚刚更改了我的depts.jsp ajax调用数据:属性如下所示 data:deptName:$('#deptName').val(),deptId:$('#deptId').val(),yarnCount:$('#yarnCount').val(),shiftNo:$('#shiftNo').val(),shiftInc: $('#shiftInc').val(),deptInc:$('#deptInc').val()}