2012-11-16 242 views
0

我使用Spring框架已经开始和我已经跨越了某种愚蠢的问题来到(但其实我也解决不了的话)我有控制器,它看起来像:REST Web服务

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

但保证HTML文件我只有字符串“页面”返回。我该如何解决这个问题?

+4

删除'@ ResponseBody' :)那么你是在创建一个REST服务还是一个Web App? –

+0

我不敢相信!差不多花了1.5小时!谢谢鲍里斯!关于问题,现在我正在发现Spring框架,在此之后,我将尝试创建REST服务 – Mithrand1r

回答

1

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

@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; 
}