博客信息

springboot相对SSM的开发相关场景的替代方式

发布时间:『 2019-03-01 03:10』  博客类别:SpringBoot  阅读(987)

1、监听类的替代

监听类

@WebListener
public class InitDataListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("-------------------------初始化数据-------------------------");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("-------------------------初始化数据禁止-------------------------");
    }
}


监听类加载到web容器中需要配置扫描注解

@EnableTransactionManagement
@MapperScan(value = "com.javaxl.p4.mapper")
@ServletComponentScan(value= "com.javaxl.p4.listener")
@SpringBootApplication
public class P4Application {

    public static void main(String[] args) {
        SpringApplication.run(P4Application.class, args);
    }

}


2、html页面上接受不同作用域的值

默认显示request中数据,

显示session中数据要加上 session前缀

显示servletContext中数据要加上application前缀

//装到request
request.setAttribute("requestAge", 100);
//装到session
session.setAttribute("sessionName", "小明");
//装到ServletContext
servletContext.setAttribute("applicationNum", 1);
--------------------------------------------------------------------------------------
request中: 
    <span style="color: red;" th:text="${requestAge}"></span><br />
session中: 
    <span style="color: red;" th:text="${session.sessionName}"></span><br />
servletContext中:
    <span style="color: red;" th:text="${application.applicationNum}"></span><br />


3、html上对字符串的处理

判断是不是为空:null: 
<span th:if="${name} != null">不为空</span> 
<span th:if="${name1} == null">为空</span> 
判断是不是为空字符串: “” 
<span th:if="${#strings.isEmpty(name1)}">空的</span> 
判断是否相同: 
<span th:if="${name} eq 'jack'">相同于jack,</span> 
<span th:if="${name} eq 'ywj'">相同于ywj,</span> 
<span th:if="${name} ne 'jack'">不相同于jack,</span> 
不存在设置默认值: 
<span th:text="${name2} ?: '默认值'"></span> 
是否包含(分大小写): 
<span th:if="${#strings.contains(name,'ez')}">包ez</span> 
<span th:if="${#strings.contains(name,'y')}">包j</span> 
是否包含(不分大小写) 
<span th:if="${#strings.containsIgnoreCase(name,'y')}">包j</span> 
同理。。。下面的和JAVA的String基本一样。。。。不笔记解释,官网有

${#strings.startsWith(name,'o')} 
${#strings.endsWith(name, 'o')} 
${#strings.indexOf(name,frag)}// 下标 
${#strings.substring(name,3,5)}// 截取 
${#strings.substringAfter(name,prefix)}// 从 prefix之后的一位开始截取到最后,比如 (ywj,y) = wj, 如果是(abccdefg,c) = cdefg//里面有2个c,取的是第一个c 
${#strings.substringBefore(name,suffix)}// 同上,不过是往前截取 
${#strings.replace(name,'las','ler')}// 替换 
${#strings.prepend(str,prefix)}// 拼字字符串在str前面 
${#strings.append(str,suffix)}// 和上面相反,接在后面 
${#strings.toUpperCase(name)} 
${#strings.toLowerCase(name)} 
${#strings.trim(str)} 
${#strings.length(str)} 
${#strings.abbreviate(str,10)}// 我的理解是 str截取0-10位,后面的全部用…这个点代替,注意,最小是3位

可以结合使用

<li th:each="hotSpotNews : ${application.hotSpotNewsList }"><a href="#" th:title="${hotSpotNews.title }" th:text="${#strings.length(hotSpotNews.title) < 16 ? hotSpotNews.title : #strings.substring(hotSpotNews.title, 0, 16)+'...' }">热点新闻1</a></li>


4、对日期的格式化

th:text="${#dates.format(news.publishDate, 'yyyy-MM-dd')}"


5、页面的包含

被包含的页面html片段

<div class="col-md-9" th:fragment="newsList">
    <ol class="breadcrumb badge-light" th:utext="${navCode}">
    </ol>

    <div class="datas news_type_list">
        <ul>
            <li th:each="news : ${newestNewsListWithType}">
                【<span style="color: red" th:text="${#dates.format(news.publishDate, 'yyyy-MM-dd')}"></span>】
                <a th:href="@{'/news/show/'+${news.newsId}}" th:title="${news.title }" th:text="${news.title}">最新新闻1</a>
            </li>
        </ul>
    </div>

    <ul class="pagination" th:utext="${pageCode}">
    </ul>
</div>

引用页面片段的页面

<!--class="col-md-9"可以不写,反正也是会被替换掉,但是为了代码的可读性高,还是加上-->
        <div class="col-md-9" th:replace="${mainPage} :: newsList">
        </div>

也可以引用整个页面把( :: newList)改成( :: html)即可


6、thymeleaf中事件中直接拼接js代码

 <button class="btn btn-mini btn-danger" type="button" th:onclick="'javascript:window.location.href=\''+@{/admin/link/del}+'\''">删除</button>

上面是不携带参数的写法,下面携带参数的写法

<button class="btn btn-mini btn-danger" type="button" th:onclick="'javascript:window.location.href=\''+@{'/admin/link/del/'+${linkBack.linkId}}+'\''">删除</button>


7、





关键字:     springboot  

备案号:湘ICP备19000029号

Copyright © 2018-2019 javaxl晓码阁 版权所有