我已经开始使用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>
但是HTML文件的输入我只返回了字符串“page”。我该如何解决这个问题?
答案 0 :(得分: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;
}
答案 1 :(得分:0)
本视频将引导您完成使用Spring MVC构建RESTFul服务的所有步骤,然后使用jQuery使用它。http://pluralsight.com/training/Courses/TableOfContents/springmvc-intro您还需要正确配置ContentNegotiatingViewResolver。