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

2 に答える 2

1

あなたのコードは「ページ」だけを出力します(@ResponseBodyのため)。Web ページは返されません。メソッドの出力として、「String」の代わりに「ModelAndView」を使用できます。そこに 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 に答える