springboot web开发笔记--第一天

第一天在用springboot 开发web项目的时候,作为测试的代码,从网络大神那边复制过来了部分index以及controller,七拼八凑的组成了一个能跑起来的项目。

第一个问题:

在测试controller返回的时候,出现返回的一直是string类型,而没有跳转到我想要去的html页面。

找了好多,后面确定是注解的问题。

原来的注解是@RestController,这个注解按照查询过来的说法,

/*@RestController注解,相当于@Controller和@ResponseBody的结合,用于返回json数据。

@GetMapping("/index"),相当于@RequestMapping(value = "/index", method = RequestMethod.GET),用于接收GET请求

 */

把注解改成@Controller,就可以跳转到想要的页面了~~

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @GetMapping
    public Response<Person> get(){
        Response<Person > response = new Response<>();
        Person user = new Person();
        user.setAge(25);
        user.setName("MMJJcaca");
        response.setData(user);
        return  response;
    }
}

这个是返回的json。

修改后的另一个测试代码:

@Controller
public class IndexController {
    @RequestMapping("/index")
    public String index(Model model) {
        Person single = new Person("aa", 11);
        List<Person> people = new ArrayList<>();
        Person p1 = new Person("zhangsan", 11);
        Person p2 = new Person("lisi", 22);
        Person p3 = new Person("wangwu", 33);
        people.add(p1);
        people.add(p2);
        people.add(p3);
        model.addAttribute("singlePerson", single);
        model.addAttribute("people", people);
        return "hello";
    }
}

本来是return "index"的,测试用简单的hello.html了~~

第二个问题:

好不容易能够跳转到指定页面之后,页面出现的是500.。。。

查看报错信息,发现说是:

Caused by: org.attoparser.ParseException: Only variable expressions returning numbers or booleans are allowed in this context, any other datatypes are not trusted in the context of this expression, including Strings or any other object that could be rendered as a text literal. A typical case is HTML attributes for event handlers (e.g. "onload"), in which textual data from variables should better be output to "data-*" attributes and then read from the event handler. (template: "index" - line 28, col 41) at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393) at org.attoparser.MarkupParser.parse(MarkupParser.java:257) at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230) ... 48 more Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Only variable expressions returning numbers or booleans are allowed in this context, any other datatypes are not trusted in the context of this expression, including Strings or any other object that could be rendered as a text literal. A typical case is HTML attributes for event handlers (e.g. "onload"), in which textual data from variables should better be output to "data-*" attributes and then read from the event handler. (template: "index" - line 28, col 41)

英文不好,也不知道说的是啥,但是看到了定位:(template: "index" - line 28, col 41),有定位那就好办了,找到对应的代码,发现指向的是th:onclick标签。

但是在IDEA上又没有任何的报错信息,真的好无奈,只能各种百度。

原先的写法是第一种,就是获得名字1:

<!--                <button class="btn" th:οnclick=" 'getName(\'' +${person.name}+ '\'); '">获得名字1</button>-->
<!--                <button class="btn" th:οnclick="'javascript:getName(\''+${person.name}+'\')'">获得名字2</button>-->
                    <button class="btn"  th:οnclick="getName([[${person.name}]])">获得名字3</button>

百度了第二种,还有一种是说写th:οnclick="|getName(${person.name} )|"和th:οnclick="'getName(' + ${person.name} + ');'";

上面的获得名字3方法是我页面能正常展示的,点击按钮,控制台也有相应输出

搜的越多,发现问题越来越明显,应该是跟thymeleaf的版本有关系了,因为是小白,第一次接触,所以使用的是当前最新版本thymeleaf 3.0.11.RELEASE,再看看,之前搜到的很多都是在17年,18年前的大佬写的了,后来看到https://blog.csdn.net/u010592926/article/details/86373871 这个,里面给出了我遇到的情况,也给出了我能正确输出界面的写法,突发奇想,既然有版本更新,那我去看看官方文档是怎么写的么。。

于是乎,https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#aggregates,我打开了官方文档,查找了th:onclick

被归类了。。。好吧,我应该再仔细的往前细细看文档了~~(英语太差,只能慢慢看,希望能有朝一日看得过来。。。)

虽然第二个问题在页面上被解决了,但是在IDEA里面,还是有个问题

这个红色波浪线在这里是要逼死强迫症啊。。。明明页面没有报错信息了,怎么这里来一出这个。。。希望有大神能够解答一下。。。我目前还在尝试,没能成功~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值