网站建设费用设计,溧水区城乡建设局网站,南昌公司网站开发,做塑胶网站需要什么材料web开发demo#xff1a;点击查看 LearnSpringBoot05Web
点击查看更多的SpringBoot教程
技术摘要
webjarsBootstrap模板引擎thymeleaf嵌入式Servlet容器注册web三大组件
一、webjars webjars官网 简介 简介翻译 WebJars 是打包到 JAR#xff08;Java Archive#xff09;…web开发demo点击查看 LearnSpringBoot05Web
点击查看更多的SpringBoot教程
技术摘要
webjarsBootstrap模板引擎thymeleaf嵌入式Servlet容器注册web三大组件
一、webjars webjars官网 简介 简介翻译 WebJars 是打包到 JARJava Archive文件中的客户端 Web 库例如 jQuery 和 Bootstrap。 显式且轻松地管理基于 JVM 的 Web 应用程序中的客户端依赖项 使用基于 JVM 的构建工具例如 Maven、Gradle、sbt…下载客户端依赖项 了解您正在使用哪些客户端依赖项 传递依赖项会自动解析并通过 RequireJS 选择性加载 部署在 Maven 中心 公共 CDN。 pom.xml添加webjars依赖 pom.xml添加webjars依赖代码 !--https://www.webjars.org/WebJars介绍 https://blog.csdn.net/Adrian_Dai/article/details/80505076http://localhost:8084/webjars/jquery/3.6.4/jquery.js 访问webjars的jquery静态资源注意http://localhost:8084/crud访问地址后面加/crud因为在application.properties里配置了访问路径 server.servlet.context-path/crudhttp://localhost:8084/crud/webjars/jquery/3.6.4/jquery.js 访问webjars的jquery静态资源--dependencygroupIdorg.webjars/groupIdartifactIdjquery/artifactIdversion3.6.4/version/dependency!--https://www.webjars.org/ 里找到 bootstrapbootstrap官网https://getbootstrap.com/bootstrap中文网https://www.bootcss.com/https://github.com/twbs/bootstraphttp://localhost:8084/webjars/bootstrap/4.0.0/js/bootstrap.js 访问webjars的bootstrap静态资源注意http://localhost:8084/crud访问地址后面加/crud因为在application.properties里配置了访问路径 server.servlet.context-path/crudhttp://localhost:8084/crud/webjars/bootstrap/4.0.0/js/bootstrap.js 访问webjars的bootstrap静态资源--dependencygroupIdorg.webjars/groupIdartifactIdbootstrap/artifactId
!-- version5.3.1/version--version4.0.0/version/dependency项目启动后访问webjars的jquery的jquery.js静态资源 项目启动后访问webjars的bootstrap的js/bootstrap.js静态资源 二、Bootstrap Bootstrap官网 Bootstrap中文网 Github里的Bootstrap Bootstrap是美国Twitter公司的设计师Mark Otto和Jacob Thornton合作基于HTML、CSS、JavaScript 开发的简洁、直观、强悍的前端开发框架使得 Web 开发更加快捷。Bootstrap提供了优雅的HTML和CSS规范它即是由动态CSS语言Less写成。 三、模板引擎thymeleaf thymeleaf官网 Github里thymeleaf 简介 简介翻译 Thymeleaf是一个现代的服务器端Java模板引擎适用于web和独立环境。 Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板——HTML可以在浏览器中正确显示也可以作为静态原型工作允许开发团队之间更强的协作。 有了Spring框架的模块与你最喜欢的工具的大量集成以及插入你自己的功能的能力Thymeleaf是现代HTML5 JVM web开发的理想选择——尽管它还有更多的功能。 添加spring-boot-starter-thymeleaf依赖 Spring-Boot的starters文档找到spring-boot-starter-thymeleaf pom.xml添加spring-boot-starter-thymeleaf依赖代码 !--spring-boot-starter-thymeleafhttps://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using.build-systems.startershttps://github.com/thymeleaf/thymeleafTemplateEngineConfigurationsorg.springframework.boot.autoconfigure.thymeleaf把html页面放在 classpath:/templates/ thymeleaf就可以自动渲染了--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId/dependency把html页面放在 classpath:/templates/ thymeleaf就可以自动渲染了 四、嵌入式Servlet容器 MyServeltConfig.java类里配置嵌入式的servlet容器 查看支持嵌入其他servlet MyServeltConfig.java代码
package com.example.learnspringboot05web.config;import com.example.learnspringboot05web.filter.MyFilter;
import com.example.learnspringboot05web.listener.Mylistener;
import com.example.learnspringboot05web.servlet.Myservlet;
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.lang.management.MemoryUsage;
import java.lang.reflect.Array;
import java.util.Arrays;Configuration
public class MyServeltConfig {//注册三大组件Beanpublic ServletRegistrationBean myServlet() {//http://localhost:8084/crud/myServletServletRegistrationBean servletRegistrationBean new ServletRegistrationBean(new Myservlet(), /myServlet);return servletRegistrationBean;}Beanpublic FilterRegistrationBean filterRegistrationBean() {FilterRegistrationBean filterRegistrationBean new FilterRegistrationBean();filterRegistrationBean.setFilter(new MyFilter());filterRegistrationBean.setUrlPatterns(Arrays.asList(/hello, /myServlet));return filterRegistrationBean;}Beanpublic ServletListenerRegistrationBean mylistener(){ServletListenerRegistrationBeanMylistener registrationBean new ServletListenerRegistrationBeanMylistener(new Mylistener());return registrationBean;}//配置嵌入式的servlet容器/*https://www.jianshu.com/p/b973476ccfd6 https://blog.csdn.net/qq_43843951/article/details/108049897Spring Boot2.0以上版本EmbeddedServletContainerCustomizer被WebServerFactoryCustomizer替代*/Beanpublic WebServerFactoryCustomizerConfigurableJettyWebServerFactory webServerFactoryWebServerFactoryCustomizer() {return new WebServerFactoryCustomizerConfigurableJettyWebServerFactory() {//定制嵌入式的servlet容器相关规则Overridepublic void customize(ConfigurableJettyWebServerFactory factory) {factory.setPort(8082);}};}
}
五、注册web三大组件 web三大组件分别是 ServletFilter: 过滤器Listener 监听器 对应的Bean类 ServletRegistrationBeanFilterRegistrationBeanServletListenerRegistrationBean 在MyServeltConfig.java类里注册三大组件 Myservlet.java代码
package com.example.learnspringboot05web.servlet;import com.sun.source.util.DocSourcePositions;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;import java.io.IOException;public class Myservlet extends HttpServlet {//http://localhost:8084/crud/myServlet//处理get请求Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter().write(hello Myservlet);}
}
Mylistener.java代码
package com.example.learnspringboot05web.listener;import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;public class Mylistener implements ServletContextListener {Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println(Mylistener 执行了 contextInitialized web应用启动);}Overridepublic void contextDestroyed(ServletContextEvent sce) {System.out.println(Mylistener 执行了 contextDestroyed web项目销毁);}
}
MyFilter.java代码
package com.example.learnspringboot05web.filter;import jakarta.servlet.*;import java.io.IOException;public class MyFilter implements Filter {Overridepublic void init(FilterConfig filterConfig) throws ServletException {Filter.super.init(filterConfig);}Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {System.out.println(MyFilter 执行了 doFilter );chain.doFilter(request, response );}Overridepublic void destroy() {Filter.super.destroy();}
} Mylistener组件监听结果 Servlet和MyFilter组件回调 项目启动后在浏览器地址栏访问http://localhost:8084/crud/myServlet web获取到Servlet组件写入的数据 MyFilter组件回调 六、bootstrap和thymeleaf配合使用的效果 项目启动后在浏览器地址栏里访问http://localhost:8084/crud/ 账户admin密码123456 登录成功进入主页 员工管理页面 编辑页面 添加员工页面 application.properties代码 #查看 ServerProperties 源码server.port8084
#spring.web.resources.static-locationsclasspath:/hello, classpath:/test# 禁止混存 修改后 按住 commd f9 重新编译
spring.thymeleaf.cachefalse# 访问路径必须是 crud ? http://localhost:8084/crud/
server.servlet.context-path/crud
# 绑定国际化 从 i18n文件里读取
spring.messages.basenamei18n.login#默认的格式dd/MM/yyyy
spring.mvc.format.dateyyyy-MM-dd#SpringBoot-RuntimeException缺少exception和message问题解决方法, 这个问题是由于SpringBoot2.0以上版本需要在配置文件中指定server的异常对象和异常信息
# https://blog.csdn.net/qq_40898909/article/details/126346500 https://blog.csdn.net/qq_33879627/article/details/106621563
# 查看 ErrorProperties 源码 private IncludeAttribute includeMessage IncludeAttribute.NEVER; includeException 默认 false
server.error.include-exceptiontrue
server.error.include-messagealwayspom.xml代码
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.1.1/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.example/groupIdartifactIdLearnSpringBoot05Web/artifactIdversion0.0.1-SNAPSHOT/versionnameLearnSpringBoot05Web/namedescriptionLearnSpringBoot05Web/descriptionpropertiesjava.version17/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--引入其他servletjetty启动java.lang.ClassNotFoundException: jakarta.servlet.http.HttpSessionContext解决方法pom文件中添加依赖jakarta.servletjakarta.servlet-api5.0.0原因此时的jetty是11.x版本的不支持jakarta.servlet-api 6改成5即可原文链接https://blog.csdn.net/weixin_44866139/article/details/132006996--
!-- dependency--
!-- groupIdorg.springframework.boot/groupId--
!-- artifactIdspring-boot-starter-web/artifactId--
!-- exclusions--
!-- exclusion--
!-- groupIdorg.springframework.boot/groupId--
!-- artifactIdspring-boot-starter-tomcat/artifactId--
!-- /exclusion--
!-- /exclusions--
!-- /dependency--!-- dependency--
!-- groupIdorg.springframework.boot/groupId--
!-- artifactIdspring-boot-starter-undertow/artifactId--
!--lt;!ndash; artifactIdspring-boot-starter-jetty/artifactIdndash;gt;--
!-- undertow 和 jetty 二选一 --
!-- /dependency--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!--https://www.webjars.org/WebJars介绍 https://blog.csdn.net/Adrian_Dai/article/details/80505076http://localhost:8084/webjars/jquery/3.6.4/jquery.js 访问webjars的jquery静态资源注意http://localhost:8084/crud访问地址后面加/crud因为在application.properties里配置了访问路径 server.servlet.context-path/crudhttp://localhost:8084/crud/webjars/jquery/3.6.4/jquery.js 访问webjars的jquery静态资源--dependencygroupIdorg.webjars/groupIdartifactIdjquery/artifactIdversion3.6.4/version/dependency!--https://www.webjars.org/ 里找到 bootstrapbootstrap官网https://getbootstrap.com/bootstrap中文网https://www.bootcss.com/https://github.com/twbs/bootstraphttp://localhost:8084/webjars/bootstrap/4.0.0/js/bootstrap.js 访问webjars的bootstrap静态资源注意http://localhost:8084/crud访问地址后面加/crud因为在application.properties里配置了访问路径 server.servlet.context-path/crudhttp://localhost:8084/crud/webjars/bootstrap/4.0.0/js/bootstrap.js 访问webjars的bootstrap静态资源--dependencygroupIdorg.webjars/groupIdartifactIdbootstrap/artifactId
!-- version5.3.1/version--version4.0.0/version/dependency!--spring-boot-starter-thymeleafhttps://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using.build-systems.startershttps://github.com/thymeleaf/thymeleafTemplateEngineConfigurationsorg.springframework.boot.autoconfigure.thymeleaf把html页面放在 classpath:/templates/ thymeleaf就可以自动渲染了--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project