- 浏览: 189385 次
- 性别:
- 来自: 南京
文章分类
最新评论
之前一篇文章写了个简单的Spring mvc例子,界面表示层用的是jsp,本篇文章结合thymeleaf将表示层改成html。和用jsp不一样的地方会加特殊说明。
同样本示例也是在内网环境下,没有采用maven,直接将jar包放在工程里面。
一、配置web.xml(和采用jsp作为表示层没有任何改动),主要作用
1.定义了一个servlet拦截器,以及Mapping规则
2.引入spring-context配置文件
3.添加log4j配置文件
具体文件:
二、DispatcherServlet Context配置文件,作用
1.激活注解配置功能
2.配置资源文件路径
3.配置视图层技术
4.引入具体组件配置文档
具体文件:
相比较于利用jsp作为视图,利用html主要是引入thymeleaf对应的模版渲染类。
三、组件配置文档,主要作用
1.配置默认页面路径
2.配置组件扫描路径
具体文件
四、controller代码
html代码:
最终的工程目录:
通过和前一遍用jsp作为视图层做比较,可以发现,采用html作为视图层,只是增加了上图中红框内的几个依赖的jar,然后修改下配置文件Servlet-context.xml中的视图模版,代码完全不用做改变,所以采用spring mvc可以方便的在不同的视图层技术间转换。
工程的具体代码请参考附件
同样本示例也是在内网环境下,没有采用maven,直接将jar包放在工程里面。
一、配置web.xml(和采用jsp作为表示层没有任何改动),主要作用
1.定义了一个servlet拦截器,以及Mapping规则
2.引入spring-context配置文件
3.添加log4j配置文件
具体文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SpringTest</display-name> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/resources/log4j/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Disables Servlet Container welcome file handling. Needed for compatibility with Servlet 3.0 and Tomcat 7.0 --> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
二、DispatcherServlet Context配置文件,作用
1.激活注解配置功能
2.配置资源文件路径
3.配置视图层技术
4.引入具体组件配置文档
具体文件:
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven> </annotation-driven> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory --> <resources mapping="/resources/**" location="/resources/" /> <beans:bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".html" /> <!-- Template cache is true by default. Set to false if you want --> <!-- templates to be automatically updated when modified. --> <beans:property name="cacheable" value="false" /> </beans:bean> <!-- SpringTemplateEngine automatically applies SpringStandardDialect and --> <!-- enables Spring's own MessageSource message resolution mechanisms. --> <beans:bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"> <beans:property name="templateResolver" ref="templateResolver" /> </beans:bean> <beans:bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver"> <beans:property name="templateEngine" ref="templateEngine" /> </beans:bean> <!-- Imports user-defined @Controller beans that process client requests --> <beans:import resource="controllers.xml" /> </beans:beans>
相比较于利用jsp作为视图,利用html主要是引入thymeleaf对应的模版渲染类。
三、组件配置文档,主要作用
1.配置默认页面路径
2.配置组件扫描路径
具体文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Maps '/' requests to the 'home' view --> <mvc:view-controller path="/" view-name="views/hello.html"/> <context:component-scan base-package="test.spring.mvc"/> </beans>
四、controller代码
//** * */ package test.spring.mvc; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; /** * @author Administrator * */ @Controller @RequestMapping("/mvc") public class MCVController { private static final Log logger = LogFactory.getLog(MCVController.class); @RequestMapping("/hello") public String hello(){ logger.info("start to maping request hell"); return "hello"; } @RequestMapping(value = "/test",method =RequestMethod.GET) public @ResponseBody String test(){ return "abc"; } @RequestMapping(value = "/test2",method =RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE) public @ResponseBody AbcTest test2(){ AbcTest test = new AbcTest(); test.setAge("20"); test.setName("me"); return test; } }
html代码:
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>html</title> </head> <body> <div style = "width: 200px;height: 100px;color: red">hello</div> <button id='buttontest' style = "width: 50px;height: 50px;color: blue"></button> <div> <label>test:</label> <span id="test"></span> </div> <div> <label>test2:</label> <span id="test2"></span> </div> </body> <script type="text/javascript" th:src="@{/resources/js/jquery-2.0.3.js}"></script> <script type="text/javascript"> $('#buttontest').click(function() { $.ajax({ type: "GET", url: "test", /* headers:{ Accept: "text/plain" }, */ success: function(msg){ console.log( "Data Saved: " + msg ); $("#test").text(msg); }, error:function(e){ alert(e) } }); $.ajax({ type: "GET", url: "test2", success: function(msg){ console.log( "Data Saved: " + msg ); $("#test2").text("age:"+msg.age+",name"+msg.name); }, error:function(e){ alert(e) } }); }) </script> </html>
最终的工程目录:
通过和前一遍用jsp作为视图层做比较,可以发现,采用html作为视图层,只是增加了上图中红框内的几个依赖的jar,然后修改下配置文件Servlet-context.xml中的视图模版,代码完全不用做改变,所以采用spring mvc可以方便的在不同的视图层技术间转换。
工程的具体代码请参考附件
发表评论
-
spring-session-redis 实现原理
2019-03-20 19:24 15611.实现原理 1.1 得益于HttpSession和HttpS ... -
spring-security
2018-01-08 09:05 0FilterSecurityInterceptor是一个fil ... -
Spring cloud config利用gitlab webhook自动刷新
2017-02-10 20:14 27一、简介 Spring Cloud Config 提供serv ... -
Spring @ConfigurationProperties
2017-02-10 20:27 7791.配置文件类 package chengf.spring ... -
Spring 利用@Value注入properties文件属性
2017-01-21 09:05 1399本编文章是对Spring利用@Value来直接注入proper ... -
Spring ws 小示例
2016-12-10 15:43 4238一、简介 Spring Web Service 致力于开发 ... -
Spring4+WebSocket小示例
2016-11-27 11:09 2530一、简介 WebSocket ... -
Spring+Schedule(定时任务)小示例
2016-11-18 17:21 1366本篇文章简单介绍一个Spring的schedule小例子。此定 ... -
SpringMVC小示例
2016-11-16 20:23 651***因为在内网环境下, ...
相关推荐
thymeleaf,我个人认为是个比较好的模板,性能也比一般的,比如freemaker的要高,而且把将美工和程序员能够结合起来,美工能够在浏览器中查看静态效果,程序员可以在应用服务器查看带数据的效果。 thymeleaf是一个...
springboot+mybatis+mysql+layUI+thymeleaf技术整合,实现对学生信息的增删改查
主要介绍了Spring boot+mybatis+thymeleaf 实现登录注册增删改查功能的示例代码,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
使用idea 结合spring与thymeleaf整合创建的一个小示例
目标:本示例说明SFM(SpringBoot+WebFlux+Mybatis)单体高并应用开发。 开发环境:IDEA集成工具,JDK 1.8 使用步骤:下载资源后解压项目,使用IDEA导入项目文件。 使用步骤: 1、下载加压后,使用IDEAl导入项目 2、...
弹簧靴在该项目中,您可以看到如何在Spring Boot应用程序中配置spring安全性(通过UserDetailsService实现)和thymeleaf模板引擎,以及如何在网页上使用spring security名称空间。
主要介绍了Spring Boot 整合 Shiro+Thymeleaf过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
spring-boot-jpa-thymeleaf-curd:spring boot + jpa + thymeleaf 增删改查示例 spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-web...
本篇文章主要介绍了springboot+thymeleaf国际化之LocaleResolver的示例 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
SpringBoot+SpringSecurity整合示例代码,实现了从数据库中获取信息进行登录认证和权限认证。 本项目为idea工程,请用idea2019导入(老版应该也可以)。 本项目用户信息所需sql文件,在工程的resources文件夹下,...
主要介绍了Spring boot2+jpa+thymeleaf实现增删改查,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
spring-boot-jpa-thymeleaf-crud Spring Boot+JPA+Thymeleaf实现增删改查示例 spring-boot-mybatis-thymeleaf-crud Spring Boot+MyBatis+Thymeleaf实现增删改查示例 spring-boot-docker Sping Boot使用远程Docker...
这是SpringSource的启用了百里香叶的Spring PetClinic官方示例应用程序。 原始应用程序位于在本地运行诊所首先,您必须下载它。 如果您在本地安装了git,则只需克隆此存储库即可:git clone ...
spring-boot-jpa-thymeleaf-curd:spring boot + jpa + thymeleaf 增删改查示例 spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-...
## Spring Boot + Thymeleaf + Redis + Heroku ##这个演示应用程序已创建为在Heroku上部署Spring Boot + Thymeleaf + Redis的示例。 ###技术堆栈### Spring Boot,用于Servlet 3.0环境的no-xml Spring MVC 4 Web...
spring.thymeleaf.prefix=classpath:/templates/ #spring.thymeleaf.suffix=.jsp spring.thymeleaf.cache=false #mybatis 配置 mybatis.mapper-locations=classpath:mapping/*Mapper.xml mybatis.type-...
技术栈:SpringBoot,Thymeleaf,MySQL,MyBatis-Plus,Lombok,Gson,咖啡因,验证,Bootstrap,jQuery,FontAwesome,Jsoup……示例博客:快速开始下载本项目,并使用IDE打开新建数据库博客并运行项目现在运行项目...
使用 Spring Boot、Bootstrap 和 Thymeleaf 的示例项目。 有关更多信息,请阅读博客文章: : 安装 克隆这个项目 git clone ...
spring-boot2.0 thymeleaf bootstrap 整合的一个示例项目,使用idea开发。步骤如下: 首先在idea或者eclipse(STS)中新建springboot项目,在选择依赖(Dependences)时引入web和thymeleaf。 接着我们将新建需要用到的...