Java:如何使用Spring加载HTML页面?
我尝试使用Spring加载index.html。但是当我在localhost中加载它而不是得到我的HTML页面时,我只得到一个表示index的字符串 我尝试用IndexController加载索引Java:如何使用Spring加载HTML页面?,java,spring-mvc,spring-rest,Java,Spring Mvc,Spring Rest,我尝试使用Spring加载index.html。但是当我在localhost中加载它而不是得到我的HTML页面时,我只得到一个表示index的字符串 我尝试用IndexController加载索引 @Controller public class IndexController { @RequestMapping("/") @ResponseBody public String welcome() { return "index"; }
@Controller
public class IndexController {
@RequestMapping("/")
@ResponseBody
public String welcome() {
return "index";
}
}
尝试使用重定向视图:
@Controller
public class IndexController {
@RequestMapping("/")
@ResponseBody
public RedirectView welcome() {
//@FIXME load url from propertie
return new RedirectView("http://localhost/index.html");
}
}
问题在于方法上的@ResponseBody注释。它告诉Spring应该序列化方法的返回值并将其发送给客户端,而不是Spring将字符串作为视图名称处理 您必须进行两项更改
@ResponseBody
,因为它将在正文中而不是在查看页面中发送响应检查我尝试了上述操作,但现在当我连接到它重定向到的localhost时,我发现无法从浏览器连接。用您的工作索引替换localhost/index.html,我不知道您的端口和frontpage技术。我删除了@ResponseBy,现在得到:出现意外错误(类型=内部服务器错误,状态=500)。解析模板[index]时出错,模板可能不存在,或者任何已配置的模板解析程序都无法访问模板。错误谢谢!成功了。删除了@ResponseBy并修复了拼写错误。