1 @Controller路径 @Responsebody返回值(不跳转网页/restcontroller下不用添加)
@RestContoller=@Controller +@ResponseBody
@ResquestMapping("")具体访问路径 类上“/pager”="pager"
@Controller()
@RequestMapping("pager")//添加“/”与不添加一样,都会是默认有个“/”
public class PageController {
// notice: 通过 http://localhost:8080/pager/index访问该方法
// notice: @ResponseBody:表示该方法的返回结果直接写入HTTP response body中
// notice: 在使用@RequestMapping后,返回值通常解析为跳转路径
// notice: 加上@Responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。
// notice: 比如异步获取json数据,加上@Responsebody后,会直接返回json数据。该注解一般会配合@RequestMapping一起使用
//notice 需要用到theamleaf框架加载h5页面,否则加载不出来
@RequestMapping("/index")
public String index() {
return "index";
}
@RequestMapping("/index2")
public String index2() {
return "index2";
}
}
2
//@RequestMapping("/demo")
// notice:
// notice: @RequestMapping:提供路由信息,负责URL到Controller中的具体函数的映射
@Controller
public class UserController {
@Autowired
UserMapper userMapper;
// notice: 通过 http://localhost:8080/user访问该方法
// notice: @ResponseBody:表示该方法的返回结果直接写入HTTP response body中
// notice: 在使用@RequestMapping后,返回值通常解析为跳转路径
// notice: 加上@esponsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。
// notice: 比如异步获取json数据,加上@Responsebody后,会直接返回json数据。该注解一般会配合@RequestMapping一起使用
@RequestMapping("/user")
@ResponseBody
public String insert(){
User user = new User();
user.setAge(12);
user.setName("zxl");
user.setEmail("zxl@163.com");
userMapper.insert(user);
return "ok";
}
// notice: 不加@esponsebody返回结果会被解析为跳转路径
@RequestMapping("/index")
public String index(){
return "index";
}
@PostMapping("/login")
@ResponseBody//返回json //@RequestBody接受json格式数据
public Object login(@RequestParam("userName") String userName, @RequestParam("password") String password ){//, @RequestBody String obj
User user = new User();
user.setAge(12);
user.setName(userName);
user.setEmail(password);
return user;
}
}