简体   繁体   English

Spring MVC无法映射到我的控制器

[英]Spring MVC Will Not Map to My Controller

I am trying to migrate a project from a traditional (working) Java servlet application to Spring MVC in the NetBeans IDE, but my program absolutely refuses to ping the Controller. 我正在尝试将项目从传统的(正在运行的)Java servlet应用程序迁移到NetBeans IDE中的Spring MVC,但是我的程序绝对拒绝ping Controller。 On the client side, I see the following 404 errors: 在客户端,我看到以下404错误:

index.do?displayType=table:1 Failed to load resource: the server responded with a status of 404 (Not Found) index.do?displayType=table:1无法加载资源:服务器以404状态(未找到)进行了响应

index.do?displayType=table:1 Failed to load resource: the server responded with a status of 404 (Not Found) index.do?displayType=table:1无法加载资源:服务器以404状态(未找到)进行了响应

I invoke the controller methods like so: 我像这样调用控制器方法:

$.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()));

Below are my controller and xml configuration files: 以下是我的控制器和xml配置文件:

MappingControl.java MappingControl.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);

    }

}

applicationContext.xml applicationContext.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>

dispatcher-servlet.xml dispatcher-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>

I've been building this based on the default Spring-MVC template provided by NetBeans, so if any of you believe the template is poorly formatted and should be changed in some way, I would appreciate any input you might have. 我一直在基于NetBeans提供的默认Spring-MVC模板进行构建,因此,如果你们中的任何人认为模板的格式不正确,应该以某种方式进行更改,我将不胜感激。

Thanks so much for your time, guys, and let me know if anything is unclear. 伙计们,非常感谢您的宝贵时间,如果有任何不清楚的地方,请告诉我。

You need add @ResponseBody to your method,due to in your case,you want to return the json data,if you missing @ResponseBody it will return to the view page,however you do not specify any view page in your code,thus will cause 404 error 您需要添加@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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM