Java:如何使用Spring加载HTML页面?

huangapple go评论77阅读模式
英文:

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.

My Resource structure

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

你需要进行两个更改。

  1. 删除 @ResponseBody,因为它会在消息体中发送响应,而不是一个视图页面。

  2. 文件夹名称中有一个拼写错误。将文件夹名称从 templetes 改为 templates

英文:

You have to make 2 changes.

  1. Remove @ResponseBody as it will send response in body instead of a view page.

  2. 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");
    }
}

huangapple
  • 本文由 发表于 2020年4月7日 00:53:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/61064892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定