51,411
社区成员
发帖
与我相关
我的任务
分享@RequestMapping(value = "/findAllStudent", method = RequestMethod.GET)
@ResponseBody
public String findAllStudent() {
List<Student> students = studentService.findAll();
String result = "";
for (Student s : students
) {
result = JSON.toJSONString(s) + result;
}
return result;
}

[/quote]
你看看你的页面是在resources目录下存放还是哪里,要是在resources目录下新建一个文件夹存放,需要在application.properties文件里映射访问路径及后缀:
spring.thymeleaf.prefix=classpath:/文件夹名字/
spring.thymeleaf.suffix=.html
试了不行,把@ResponseBody注了接口不能返回数据了[/quote]
你是想跳转页面的同时把数据也传过去吧,可以用作用域Model来传, model.addAttribute("xxx", result);
然后在页面通过${“xxx”}来获取数据[/quote]
就是单纯的跳转也过不去,注解注掉之后页面Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri May 17 15:44:12 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available[/quote]
那是别的原因跟这个没关系,你看看你的启动类是不是在最外层,启动类要放到Controller类的外层[/quote]
或者你可以上个图我给你看一下?
[/quote]
[/quote]
你看看你的页面是在resources下面存放还是哪里,要是在resources下新建一个文件夹如templates存放页面,需要在application.properties配置文件里映射路径及后缀名:
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html[/quote]多谢指教
[/quote]
[/quote]
@RestController
@RequestMapping("")
public class MainController {
@GetMapping("302")
public ResponseEntity Test302(){
HttpHeaders headers = new HttpHeaders();
headers.add("Location", "http://www.baidu.com/");
return new ResponseEntity( "", headers, HttpStatus.TEMPORARY_REDIRECT);
}
但是,api一般是返回json,给前端的ajax调用,你有时返回json,有时返回302跳转,给前端同学造成不必要的工作和逻辑处理。
response.addHeader("location", "https://www.baidu.com/");
response.setStatus(302);

