application.yml文件中
context-path 代表访问路径url需加上内容(也可以不加这条代码)
如http://localhost:7777/hr/MyIndex
测试过程中使用@RestController注解@RestController加在类上面的注解,使得类里面的每个方法都将json/xml返回数据加返回到前台页面中。如
@RestController
public class TestController {
@GetMapping("/MyIndex")
// @ResponseBody
public String index(){
// System.out.println(aa);
System.out.println("测试");
return "index";
// return new ModelAndView("test/index");
}
}
访问locallhost之后,页面显示为return内的内容
@Controller注解:@Controller加在类上面的注解,使得类里面的每个方法都返回一个试图页面。
@Controller
public class TestController {
@GetMapping("/MyIndex")//@GetMapping注解接口要与url中localhost:9090/MyIndex对应 不然访问不到
// @ResponseBody
public String index(){
// System.out.println(aa);
System.out.println("测试");
return "test/index"; //用@Controller注解 return要注意返回路径,默认在templates根目录下。且返回的是视图(html)文件
// return new ModelAndView("test/index");
}
}
访问界面后显示的是index.html 页面
@Controller和@ResponseBody(加在方法/类上面)一起使用,和@RestController的作用相同。
@Controller
public class TestController {
@GetMapping("/MyIndex")//@GetMapping注解接口要与url中localhost:9090/MyIndex对应 不然访问不到
@ResponseBody
public String index(){
// System.out.println(aa);
System.out.println("测试");
return "index"; //用@Controller注解 return要注意返回路径,默认在templates根目录下。且返回的是视图(html)文件
// return new ModelAndView("test/index");
}
}
注:如使用@RestController注解 一般需使用ModelAndView 用于返回视图页面
@RestController
public class TestController {
@GetMapping("/MyIndex")//@GetMapping注解接口要与url中localhost:9090/MyIndex对应 不然访问不到
// @ResponseBody
public ModelAndView index(){
// System.out.println(aa);
System.out.println("测试");
// return "index"; //用@Controller注解 return要注意返回路径,默认在templates根目录下。且返回的是视图(html)文件
return new ModelAndView("test/index");//同样view Name要注意路径
}
}
index函数括号内为入参参数,在浏览器访问的url中加上?aa=_ 用于返回后端控制台前端输入的值
@RestController
public class TestController {
@GetMapping("/MyIndex")//@GetMapping注解接口要与url中localhost:9090/MyIndex对应 不然访问不到
// @ResponseBody
public ModelAndView index(String aa){
System.out.println(aa);
System.out.println("测试");
// return "index"; //用@Controller注解 return要注意返回路径,默认在templates根目录下。且返回的是视图(html)文件
return new ModelAndView("test/index");//同样view Name要注意路径
}
}
访问 http://localhost:7777/hr/MyIndex?aa=5