Springboot用官方建议访问Html页面并接传值

 

特别强调:启动类和所有的controller service mapper等包必须位于同一个主包下(同一个包中),而且启动类在最外面,否则这些层都扫不到,不报错,但是无法实现我们的功能,要非常注意

我们以前通常习惯用webapp来放置jsp页面,但是到了Springboot中,官方建议用Static文件夹来存放及静态的资源,

用templates来存放可供访问的Html资源页面,具体的操作如下.

1.加入所需要的POM依赖

  1. <!--添加static和templates的依赖-->

  2. <dependency>

  3. <groupId>org.springframework.boot</groupId>

  4. <artifactId>spring-boot-starter-web</artifactId>

  5. </dependency>

  6. <dependency>

  7. <groupId>org.springframework.boot</groupId>

  8. <artifactId>spring-boot-starter-test</artifactId>

  9. <scope>test</scope>

  10. </dependency>

  11. <dependency>

  12. <groupId>org.springframework.boot</groupId>

  13. <artifactId>spring-boot-starter-thymeleaf</artifactId>

  14. </dependency>

pom的依赖添加完成后会在resources的文件夹下面生成Static和templates的文件夹

2.增加yml文件配置

  1. spring

  2. thymeleaf:

  3. prefix: classpath:/templates/

3.在templates中添加html的页面:

index.html:

  1. <!DOCTYPE html>

  2. <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">

  3. <head>

  4. <meta charset="UTF-8">

  5. <title>第一个HTML页面</title>

  6. </head>

  7. <body>

  8. <h1>Hello Spring Boot!!!</h1>

  9. <p th:text="${hello}"></p>

  10. <div>

  11. <p th:text="${say}"></p>

  12. </div>

  13. </body>

  14. </html>

4.编写controller层

HelloController:

  1. import org.springframework.stereotype.Controller;

  2. import org.springframework.ui.Model;

  3. import org.springframework.web.bind.annotation.RequestMapping;

  4. import java.util.HashMap;

  5. @Controller //注意这里必须为Controller

  6. public class HelloController {

  7. /**

  8. * 本地访问内容地址 :http://localhost:8080/hello

  9. * @param map

  10. * @return

  11. */

  12. @RequestMapping("/hello")

  13. public String helloHtml(HashMap<String, Object> map, Model model) {

  14. model.addAttribute("say","欢迎欢迎,热烈欢迎");

  15. map.put("hello", "欢迎进入HTML页面");

  16. return "index";

  17. }

  18. }

5.完成后启动项目,访问http://localhost:8080/hello,能看到如下页面:

12057079-1f35f40da66a3942.png

image.png

这里static主要存放css js等静态资源文件 不做过多的讲述,主要来讲讲templates中html的Thymeleaf的属性,这里也是困扰我一段时间的地方,当然Springboot用Thymeleaf的原因主要是为了简化代码,用习惯了其实都挺不错的.

static下的静态页面:

static.html:

  1. <!DOCTYPE html>

  2. <html lang="en">

  3. <head>

  4. <meta charset="UTF-8">

  5. <title>Title</title>

  6. </head>

  7. <body>

  8. <h2>这是一个静态页面 可以直接访问!</h2>

  9. </body>

  10. </html>

直接访问http://localhost:8080/static.html即可:

12057079-5a302753550bacb1.png

image.png

6.动态templates下存放的页面常用的th标签

 
  1. 常用th标签都有那些?

  2.  
  3. 关键字   功能介绍     案例

  4. th:id   替换id      <input th:id="'xxx' + ${collect.id}"/>

  5. th:text  文本替换     <p th:text="${collect.description}">description</p>

  6. th:utext 支持html的文本替换 <p th:utext="${htmlcontent}">conten</p>

  7. th:object 替换对象     <div th:object="${session.user}">

  8. th:value 属性赋值     <input th:value="${user.name}" />

  9. th:with 变量赋值运算     <div th:with="isEven=${prodStat.count}%2==0"></div>

  10. th:style 设置样式         th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"

  11. th:onclick 点击事件       th:οnclick="'getCollect()'"

  12. th:each 属性赋值         tr th:each="user,userStat:${users}">

  13. th:if 判断条件         <a th:if="${userId == collect.userId}" >

  14. th:unless 和th:if判断相反     <a th:href="@{/login}" th:unless=${session.user != null}>Login</a>

  15. th:href 链接地址           <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> />

  16. th:switch 多路选择 配合th:case 使用 <div th:switch="${user.role}">

  17. th:case th:switch的一个分支     <p th:case="'admin'">User is an administrator</p>

  18. th:fragment 布局标签,定义一个代码片段,方便其它地方引用 <div th:fragment="alert">

  19. th:include 布局标签,替换内容到引入的文件 <head th:include="layout :: htmlhead" th:with="title='xx'"></head> />

  20. th:replace 布局标签,替换整个标签到引入的文件 <div th:replace="fragments/header :: title"></div>

  21. th:selected selected选择框 选中 th:selected="(${xxx.id} == ${configObj.dd})"

  22. th:src 图片类地址引入       <img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" />

  23. th:inline 定义js脚本可以使用变量 <script type="text/javascript" th:inline="javascript">

  24. th:action 表单提交的地址     <form action="subscribe.html" th:action="@{/subscribe}">

  25. th:remove 删除某个属性     <tr th:remove="all">

  26.                     1.all:删除包含标签和所有的孩子。

  27.                     2.body:不包含标记删除,但删除其所有的孩子。

  28.                     3.tag:包含标记的删除,但不删除它的孩子。

  29.                     4.all-but-first:删除所有包含标签的孩子,除了第一个。

  30.                     5.none:什么也不做。这个值是有用的动态评估。

  31. th:attr 设置标签属性,多个属性可以用逗号分隔 比如 th:attr="src=@{/image/aa.jpg},title=#{logo}",此标签不太优雅,一般用的比较少。

  32.  

6.1 th:text

可对表达式或变量求值,并将结果显示在其被包含的 html 标签体内替换原有html文本。

文本链接: 用 "+" 符号,若是变量表达式也可以用“|”符号

eg.

<p th:text="#{home.welcome}">Welcome to our grocery store!</p>

equals.(局限:只能在html5中使用)

<p data-th-text="#{home.welcome}">Welcome to our grocery store!</p>
  • The th:text attribute, which evaluates its value expression and sets the result as the body of the host tag, effectively replacing the “Welcome to our grocery store!” text we see in the code.(th:text属性,他声明设置表达式的值,并使表达式返回的值来填充标签内容,替换或设置标签内部的内容,当前例子中即替换“欢迎光临本店”这些字。)
  • The #{home.welcome} expression, specified in the Standard Expression Syntax, instructing that the text to be used by the th:text attribute should be the message with the home.welcome key corresponding to whichever locale we are processing the template with.(#{home.welcome}表达式,一个标准的表达式语法,指出在模板中,th:text属性所对应Message的key,即使用home.welcome对应的value替换现有内容。)

6.2 th:utext(非转义文本:unescaped text)

e.g.(想要输出转义字符效果)

home.welcome=Welcome to our <b>fantastic</b> grocery store!  

执行此模板,默认使用<p th:text="#{home.welcome}"></p>来解析,结果为:

<p>Welcome to our &lt;b&gt;fantastic&lt;/b&gt; grocery store!</p>

解决方案:(This is the default behaviour of the th:text attribute. If we want Thymeleaf to respect our HTML tags and not escape them, we will have to use a different attribute: th:utext (for “unescaped text”):)

使用<p th:utext="#{home.welcome}"></p>即可。

<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>

等效于html:

<p>Welcome to our <b>fantastic</b> grocery store!</p>

6.3 th:href

@{xxx} :链接url的表达式

<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,有多种方法可以实现页面之间的值传递。以下是两种常用的方法: 1. 使用URL参数传递值: 可以通过在URL中添加参数来传递值。在发送请求时,将值作为参数添加到URL中,然后在接收请求的页面中使用@RequestParam注解来获取参数的值。 ```java // 发送请求时传递值 @GetMapping("/page1") public String page1(Model model) { String value = "Hello"; model.addAttribute("value", value); return "page1"; } // 接收请求时获取参数的值 @GetMapping("/page2") public String page2(@RequestParam("value") String value, Model model) { model.addAttribute("value", value); return "page2"; } ``` 2. 使用Session传递值: 可以使用Session对象来在页面之间传递值。在发送请求时,将值存储在Session中,然后在接收请求的页面中使用Session对象来获取值。 ```java // 发送请求时存储值到Session中 @GetMapping("/page1") public String page1(HttpSession session) { String value = "Hello"; session.setAttribute("value", value); return "page1"; } // 接收请求时从Session中获取值 @GetMapping("/page2") public String page2(HttpSession session, Model model) { String value = (String) session.getAttribute("value"); model.addAttribute("value", value); return "page2"; } ``` 这些方法可以根据具体的需求选择使用。如果只需要传递简单的值,使用URL参数传递值是比较方便的。如果需要传递复杂的对象或多个值,使用Session传递值可能更合适。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值