@RestController和@Controller的区别(跳转界面)

@RestController注解相当于@ResponseBody + @Controller合在一起的作用。

  1. 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
@RestController
public class Thymeleaf {

    @GetMapping("/thymeleaf")
    public String thymeleaf(Model model){
        //model中的数据会被放在请求域中request.setAtttbute();
        model.addAttribute("msg","恭喜开启");
        model.addAttribute("link","www.baidu.com");
        return "thymeleaf";
    }
}

在这里插入图片描述

  1. 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
    如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
@Controller
@ResponseBody
public class Thymeleaf {
   @GetMapping("/thymeleaf")
   public String thymeleaf(Model model){
       //model中的数据会被放在请求域中request.setAtttbute();
       model.addAttribute("msg","恭喜开启");
       model.addAttribute("link","www.baidu.com");
       return "thymeleaf";
   }
}

在这里插入图片描述

Vue.js SpringBoot 结合可以构建高效的前后端分离应用。在登录界面的设计上,通常的做法是: 1. **前端** (Vue):用户在浏览器页面看到的是Vue应用,登录表单由HTML、CSSVue组件构成。你可以使用Vue Router管理路由,如在登录成功后跳转到首页。`axios` 或者 `vue-resource` 这样的库会被用来发送HTTP请求到SpringBoot后端。 ```html <!-- Vue登录组件 --> <template> <div> <form @submit.prevent="handleSubmit"> <input v-model="username" placeholder="用户名"/> <input type="password" v-model="password" placeholder="密码"/> <button>Login</button> </form> </div> </template> <script> export default { data() { return { username: '', password: '' } }, methods: { handleSubmit() { this.$http.post('/api/login', { username: this.username, password: this.password }) .then(response => { // 登录成功处理 }) .catch(error => { // 登录失败处理 }); } } } </script> ``` 2. **后端** (SpringBoot):创建一个处理登录请求的Controller,这里可以使用Spring Security来进行身份验证。当收到前端的登录数据后,后端会检查用户信息是否有效,并返回相应的响应(如JWT token)。 ```java import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class LoginController { @PostMapping("/login") public String authenticate(@RequestBody LoginRequest loginRequest) { // 实现登录逻辑,如果验证通过,生成并返回token } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值