英文:
Java: How to load an HTML page with Spring?
问题
以下是翻译好的内容:
我尝试使用Spring加载index.html。但是当我在本地主机上加载它时,我得到的不是我的HTML页面,而是一个字符串,上面写着"index"。
【我的资源结构】(链接已省略)
我尝试使用IndexController加载index页面
@Controller
public class IndexController {
@RequestMapping("/")
@ResponseBody
public String welcome() {
return "index";
}
}
英文:
I try to load the index.html using Spring. But when I load it in localhost instead of getting my HTML page I just get a string that says index.
I try to load the index with the IndexController
@Controller
public class IndexController {
@RequestMapping("/")
@ResponseBody
public String welcome() {
return "index";
}
}
答案1
得分: 1
问题出在方法上的@ResponseBody注解。它告诉Spring该方法的返回值应被序列化并发送给客户端,而不是Spring将字符串视为视图名称。
英文:
The issue is with the @ResponseBody annotation on the method. It tells Spring that the return value of the method should be serialized and sent to the client, rather than Spring treating the string as a view name.
答案2
得分: 1
你需要进行两个更改。
-
删除
@ResponseBody
,因为它会在消息体中发送响应,而不是一个视图页面。 -
文件夹名称中有一个拼写错误。将文件夹名称从 templetes 改为 templates。
英文:
You have to make 2 changes.
-
Remove
@ResponseBody
as it will send response in body instead of a view page. -
There is a typo in folder name there. Rename your folder name with templates instead of templetes.
答案3
得分: -1
Sure, here's the translation of the provided content:
尝试使用 RedirectView:
@Controller
public class IndexController {
@RequestMapping("/")
@ResponseBody
public RedirectView welcome() {
//@FIXME 从属性加载URL
return new RedirectView("http://localhost/index.html");
}
}
英文:
Try with RedirectView :
@Controller
public class IndexController {
@RequestMapping("/")
@ResponseBody
public RedirectView welcome() {
//@FIXME load url from propertie
return new RedirectView("http://localhost/index.html");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论