springboot学习记录

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

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值