0

我正在尝试将项目从传统(工作)Java servlet 应用程序迁移到 NetBeans IDE 中的 Spring MVC,但我的程序绝对拒绝 ping 控制器。在客户端,我看到以下 404 错误:

index.do?displayType=table:1 加载资源失败:服务器响应状态为 404(未找到)

index.do?displayType=table:1 加载资源失败:服务器响应状态为 404(未找到)

我像这样调用控制器方法:

$.get("index.do","displayType=table", function( data ) {
    jstring = JSON.parse(data.substr(12).slice(0, -1));  

});

$.get("index.do","displayType=pivot",function(unparsedJSON) {});

$.post("index.do","jsonString=" + JSON.stringify(hot.getData()));

以下是我的控制器和 xml 配置文件:

映射控制.java

package controllers;

import infoLoader.JsonWriter;
import infoLoader.getJSON;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.springframework.http.HttpMethod.GET;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;



@Controller
@RequestMapping("/")
public class MappingControl {

    @RequestMapping(value="/index.do", method=RequestMethod.GET)
    public String populatePivotAndSheet(@RequestParam("displayType") String type) {

        String returnedJSON = "";

        try {
             returnedJSON = getJSON.getJSON(type);
        } catch (Exception ex) {
            System.out.println("Unable to retrieve JSON");
        }

        return returnedJSON;

    }

    @RequestMapping(value="/index.do", method=RequestMethod.POST)
    public void deliverSheet(@RequestParam("jsonString") String writableJSON) {

        String returnedJSON = "";

        JsonWriter.writeJSON(writableJSON);

    }

}

应用程序上下文.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

</beans>

调度程序-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd" xmlns:mvc="http://www.springframework.org/schema/mvc">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

    <mvc:resources mapping="/resources/**" location="/resources" />
    <context:component-scan base-package="controllers" />


</beans>

我一直在基于 NetBeans 提供的默认 Spring-MVC 模板构建它,所以如果你们中的任何人认为该模板格式不正确并且应该以某种方式进行更改,我将不胜感激您的任何意见。

非常感谢您的时间,伙计们,如果有任何不清楚的地方,请告诉我。

4

1 回答 1

0

您需要添加@ResponseBody到您的方法中,由于在您的情况下,您想要返回json数据,如果您丢失@ResponseBody它会返回查看页面,但是您没有在代码中指定任何查看页面,因此会导致 404 错误

@ResponseBody
@RequestMapping(value="/index.do", method=RequestMethod.GET)
public String populatePivotAndSheet(@RequestParam("displayType") String type) {

    String returnedJSON = "";

    try {
         returnedJSON = getJSON.getJSON(type);
    } catch (Exception ex) {
        System.out.println("Unable to retrieve JSON");
    }

    return returnedJSON;

}

@ResponseBody
@RequestMapping(value="/index.do", method=RequestMethod.POST)
public void deliverSheet(@RequestParam("jsonString") String writableJSON) {

    String returnedJSON = "";

    JsonWriter.writeJSON(writableJSON);

}
于 2018-07-18T01:59:01.887 回答