Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Ajax post-Spring MVC控制器?_Java_Jquery_Ajax_Spring_Spring Mvc - Fatal编程技术网

Java Ajax post-Spring MVC控制器?

Java Ajax post-Spring MVC控制器?,java,jquery,ajax,spring,spring-mvc,Java,Jquery,Ajax,Spring,Spring Mvc,我正在试验SpringBoot,并想在Spring控制器上发布一篇Ajax文章。我是从邮递员那里测试的,但后来我一直发现这个错误。它似乎不是发布到我的控制器,而是寻找一个名为jsondata.jsp的视图。我如何解决这个问题 { "timestamp": 1499255141424, "status": 404, "error": "Not Found", "message": "/WEB-INF/view/jsondata.jsp", "path": "

我正在试验SpringBoot,并想在Spring控制器上发布一篇Ajax文章。我是从邮递员那里测试的,但后来我一直发现这个错误。它似乎不是发布到我的控制器,而是寻找一个名为jsondata.jsp的视图。我如何解决这个问题

{
    "timestamp": 1499255141424,
    "status": 404,
    "error": "Not Found",
    "message": "/WEB-INF/view/jsondata.jsp",
    "path": "/jsondata"
}

IndexController.java
package com.example.demo;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
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.client.RestTemplate;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;



    @Controller
    public class IndexController {

        @Autowired
        private WelcomeService service;
        @RequestMapping("/")
        public String home(Map<String, Object> model) {
            String msg = service.retrieveWelcomeMessage();
            model.put("message",msg);
            return "index";
        }

        @RequestMapping("/next")
        public String next(Map<String, Object> model) {
            model.put("message", "You are in new page !!");
            return "next";
        }

        @RequestMapping(value = "/jsondata", method = RequestMethod.POST,consumes = "application/json")
        public String getfeeddata(@RequestBody String info,Map<String, Object> model)
        {
            model.put("info", info);
            return "next";
        }

        @Component
        class WelcomeService {

            public String retrieveWelcomeMessage() {
                //Complex Method
                 String msgType="";
                 RestTemplate restTemplate = new RestTemplate();
               String consumeJSONString = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", String.class);
               Gson gson = new GsonBuilder().create();
                Quote r = gson.fromJson(consumeJSONString, Quote.class);
                msgType=r.getValue().getQuote();
                return msgType;
            }


        }

    }

MVCConfiguration.java
    package com.example.demo;

    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    import org.springframework.web.servlet.view.JstlView;

    @Configuration
    @EnableWebMvc
    @ComponentScan
    public class MvcConfiguration extends WebMvcConfigurerAdapter
    {
        @Override
        public void configureViewResolvers(ViewResolverRegistry registry) {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/view/");
            resolver.setSuffix(".jsp");
            resolver.setViewClass(JstlView.class);
            registry.viewResolver(resolver);
        }
    }

View
<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html lang="en">
<body>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            $('#submit').click(function ()
            {
                var info =[];
                info.push("JsonPostdata");
                $.ajax({
                    type: "post",
                    url: "/jsondata", 
                    data: JSON.stringify(info),
                    success: function(msg){      
                        console.log("success");
                    }
                });
            });

        });
    </script>
</head>
    <div>
        <div>
            <h1>Spring Boot JSP Example</h1>
            <form >
              <input type="submit" id ="submit" value="Not clicked">

            </form>
            <h2>Hello ${message}</h2>

            Click on this <strong><a href="next">link</a></strong> to visit another page.
        </div>
    </div>


</body>
</html>
{
“时间戳”:1499255141424,
“状态”:404,
“错误”:“未找到”,
“消息”:“/WEB-INF/view/jsondata.jsp”,
“路径”:“/jsondata”
}
IndexController.java
包com.example.demo;
导入java.util.Map;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Component;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.web.client.rest模板;
导入com.google.gson.gson;
导入com.google.gson.GsonBuilder;
导入com.google.gson.JsonArray;
导入com.google.gson.JsonElement;
导入com.google.gson.JsonObject;
导入com.google.gson.JsonParser;
@控制器
公共类索引控制器{
@自动连线
私人福利服务;
@请求映射(“/”)
公共字符串主页(地图模型){
字符串msg=service.retrieveWelcomeMessage();
model.put(“message”,msg);
返回“索引”;
}
@请求映射(“/next”)
公共字符串下一步(映射模型){
model.put(“消息”,“您在新页面!!”);
返回“下一步”;
}
@RequestMapping(value=“/jsondata”,method=RequestMethod.POST,consumes=“application/json”)
公共字符串getfeeddata(@RequestBody字符串信息,映射模型)
{
模型放置(“信息”,信息);
返回“下一步”;
}
@组成部分
班级福利服务{
公共字符串retrieveWelcomeMessage(){
//复合法
字符串msgType=“”;
RestTemplate RestTemplate=新RestTemplate();
字符串consumeJSONString=restTemplate.getForObject(“http://gturnquist-quoters.cfapps.io/api/random“,String.class);
Gson Gson=new GsonBuilder().create();
Quote r=gson.fromJson(consumeJSONString,Quote.class);
msgType=r.getValue().getQuote();
返回msgType;
}
}
}
MVCConfiguration.java
包com.example.demo;
导入org.springframework.context.annotation.ComponentScan;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.web.servlet.config.annotation.EnableWebMvc;
导入org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
导入org.springframework.web.servlet.config.annotation.WebMVCConfigureAdapter;
导入org.springframework.web.servlet.view.InternalResourceViewResolver;
导入org.springframework.web.servlet.view.JstlView;
@配置
@EnableWebMvc
@组件扫描
公共类MVCConfigureAdapter扩展了WebMVCConfigureAdapter
{
@凌驾
公共无效配置ViewResolver(ViewResolver注册表){
InternalResourceViewResolver解析器=新的InternalResourceViewResolver();
resolver.setPrefix(“/WEB-INF/view/”);
resolver.setSuffix(“.jsp”);
setViewClass(JstlView.class);
registry.viewsolver(解析器);
}
}
看法
$(文档).ready(函数(){
$(“#提交”)。单击(函数()
{
var信息=[];
推送信息(“JsonPostdata”);
$.ajax({
类型:“post”,
url:“/jsondata”,
数据:JSON.stringify(info),
成功:函数(msg){
控制台日志(“成功”);
}
});
});
});
SpringBootJSP示例
你好${message}
单击此可访问其他页面。

这就是
应用程序/json
方法的外观

@RequestMapping(value = "jsondata", method = RequestMethod.POST, headers="Content-Type=application/json")
public @ResponseBody String post(@RequestBody String string) {
            //process your "string" here
            //instead of string you can use other object or array of objects also. 
            return string;
}

您试图做的是向
模型添加字符串。然后,若您返回一个字符串“next”,那个么控制器将查找
next.jsp
。您不想这样吗?

您好,您能用错误跟踪更新您的问题吗?您的请求映射错误,似乎您映射到url:“/jsondata”,它只返回一个字符串,您在该方法中使用REST API调用,错误是什么?