简体   繁体   English

SpringMVC REST Web服务

[英]SpringMVC REST Web service

i am already starting using Spring framework and already i came across some kind of stupid problem ( but in fact i can't solve it) I have controller which looks like: 我已经开始使用Spring框架,已经遇到了一些愚蠢的问题(但实际上我无法解决),我的控制器看起来像:

package org.springframework.rest;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class SomeController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public String returnHtmlPage() {

        return "page";

    }

}

where page is page.jsp: page是page.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Home</title>
</head>
<body>
<h1>
    Hello world!  
</h1>

<P>  The time on the server is ${serverTime}. </P>
</body>
</html>

but insteed of HTML file i have only string "page" returned. 但是在HTML文件的指导下,我仅返回了字符串“页面”。 How can i fix the problem? 我该如何解决该问题?

your code would print out just "page" (because of @ResponseBody). 您的代码将仅打印“页面”(由于@ResponseBody)。 It does not return web page for you. 它不会为您返回网页。 you can use "ModelAndView" instead of "String" as your method output. 您可以将“ ModelAndView”而不是“ String”用作方法输出。 And set your jsp page name (=page) there. 并在那里设置您的jsp页面名称(= page)。 something like this: 像这样的东西:

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView returnHtmlPage(){
    ModelAndView model = new ModelAndView("page");
                /* here you can put anything in 'model' object that you
                   want to use them in your page.jsp file */
    return model;
}

This video will walk you through all the steps to build a RESTFul service using Spring MVC and then consume it using jQuery. 该视频将引导您完成所有使用Spring MVC构建RESTFul服务的步骤,然后使用jQuery使用它。 http://pluralsight.com/training/Courses/TableOfContents/springmvc-intro You need to have the ContentNegotiatingViewResolver configured correctly as well. http://pluralsight.com/training/Courses/TableOfContents/springmvc-intro您还需要正确配置ContentNegotiatingViewResolver。

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

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