Gus*_*avo 15 html java spring controller spring-boot
我想从控制器返回一个简单的html页面,但我只得到文件的名称而不是它的内容.为什么?
这是我的控制器代码:
@RestController
public class HomeController {
@RequestMapping("/")
public String welcome() {
return "login";
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的项目结构:
[
小智 24
@kukkuz 几乎回答了“为什么?”的问题。对于那些仍在研究“如何”、积累其他人的答案的人。
使用 RestController:
@RestController
public class MyRestController {
@RequestMapping("/")
public ModelAndView welcome() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login.html");
return modelAndView;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以为默认后缀设置应用程序参数,例如:
spring.mvc.view.suffix=.html
Run Code Online (Sandbox Code Playgroud)
在这种情况下,视图名称必须不带扩展名,如“登录”。
可以使用一些建议的百里香叶。意味着你的 pom.xml 依赖项中有类似这样的内容:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.4.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,login.html 默认情况下必须位于:resources/templates 文件夹中,并且调用类似,现在唯一的区别是视图名称,因为tymeleaf 使用.html作为默认值。
// fails by default
// NO fail if spring mvc view suffix is set in properties e.g.: spring.mvc.view.suffix=.html
// NO fail if thymeleaf is added, and there is a file login.html in a resources/templates folder.
@RequestMapping("/loginTest")
public ModelAndView loginTest () {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login");
return modelAndView;
}
Run Code Online (Sandbox Code Playgroud)
使用控制器:
@Controller
public class MyController {
//gets html from a default 'resources/public' or 'resources/static' folder
@RequestMapping(path="/welcome")
public String getWelcomePage(){
return "login.html";
}
//gets html from a default 'resources/public' or 'resources/static' folder
@RequestMapping("/welcome1")
public ModelAndView getWelcomePageAsModel() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login.html");
return modelAndView;
}
// fails with 404 resource not found by default
// NO fail, if spring mvc view suffix is set in properties e.g.: spring.mvc.view.suffix=.html
// NO fail, if thymeleaf is added, and there is a file login.html in a resources/templates folder
@RequestMapping(path="/welcome2")
public String thisFails(){
return "login";
}
}
Run Code Online (Sandbox Code Playgroud)
kuk*_*kuz 22
当这样使用时@RestController:
@RestController
public class HomeController {
@RequestMapping("/")
public String welcome() {
return "login";
}
}
Run Code Online (Sandbox Code Playgroud)
这与普通控制器中的相同:
@Controller
public class HomeController {
@RequestMapping("/")
@ResponseBody
public String welcome() {
return "login";
}
}
Run Code Online (Sandbox Code Playgroud)
使用@ResponseBodyreturn return "login";作为String对象.您返回的任何对象都将payload作为JSON 附加在HTTP正文中.
这就是你得到login答案的原因.
小智 7
您可以尝试使用ModelAndView:
@RequestMapping("/")
public ModelAndView index () {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
Run Code Online (Sandbox Code Playgroud)
遵循以下步骤
retunrn "index"包括以下依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>`
Run Code Online (Sandbox Code Playgroud)小智 6
我做了三件事:
@RequestMapping(method = RequestMethod.GET, value = "/")
public String aName() {
return "myPage.html";
}
Run Code Online (Sandbox Code Playgroud)
不需要特定的依赖性。
| 归档时间: |
|
| 查看次数: |
62361 次 |
| 最近记录: |