0

我已经开始使用 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";

    }

}

其中 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>

但是我只返回了字符串“page”,而不是 HTML 文件。我该如何解决这个问题?

4

2 回答 2

1

您的代码只会打印出“页面”(因为@ResponseBody)。它不会为您返回网页。您可以使用“ModelAndView”而不是“String”作为方法输出。并在那里设置您的 jsp 页面名称(=page)。像这样的东西:

@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;
}
于 2012-11-19T12:20:15.807 回答
0

该视频将引导您完成使用 Spring MVC 构建 RESTFul 服务的所有步骤,然后使用 jQuery 使用它。http://pluralsight.com/training/Courses/TableOfContents/springmvc-intro您还需要正确配置 ContentNegotiatingViewResolver。

于 2013-04-22T12:35:33.073 回答