SpringBoot(L)详解编程语言

1.创建springboot项目(war)

2.添加tomcat和jstl依赖(pom.xml)

<!-- 1.添加 --> 
         <dependency> 
            <groupId>org.apache.tomcat.embed</groupId> 
            <artifactId>tomcat-embed-jasper</artifactId> 
            <scope>provided</scope> 
        </dependency> 
        <!-- jstl --> 
        <dependency> 
            <groupId>javax.servlet</groupId> 
            <artifactId>jstl</artifactId> 
        </dependency>

3.配置springmvc前缀和后缀(application.properties)

# 前缀 
spring.mvc.view.prefix=/WEB-INF/jsp/ 
# 后缀 
spring.mvc.view.suffix=.jsp

4.日志配置(logback.xml)

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <!--定义 start  --> 
    <!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,,,, --> 
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
        <encoder> 
            <pattern>%d %p (%file:%line/)  - %m%n</pattern> 
            <charset>UTF-8</charset> 
        </encoder> 
    </appender> 
    <appender name="baselog" 
        class="ch.qos.logback.core.rolling.RollingFileAppender"> 
        <File>log/base.log</File> 
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
            <fileNamePattern>log/base.log.%d.%i</fileNamePattern> 
            <timeBasedFileNamingAndTriggeringPolicy 
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 
                <!-- or whenever the file size reaches 64 MB --> 
                <maxFileSize>64 MB</maxFileSize> 
            </timeBasedFileNamingAndTriggeringPolicy> 
        </rollingPolicy> 
        <encoder> 
            <pattern> 
                %d %p (%file:%line/)- %m%n 
            </pattern> 
            <charset>UTF-8</charset> <!-- 此处设置字符集 --> 
        </encoder> 
    </appender> 
 
    <!--定义 end  --> 
 
    <!-- 1.默认日志格式并写入文件 --> 
     <include resource="org/springframework/boot/logging/logback/base.xml"/> 
 
    <logger name="cn.wuyang.springboot" level="TRACE" > 
        <appender-ref ref="baselog" /> 
    </logger> 
</configuration>

5.中文乱码过滤器MyFilter.java

方法一

package cn.wuyang.springboot.filter; 
 
import java.io.IOException; 
 
import javax.servlet.Filter; 
import javax.servlet.FilterChain; 
import javax.servlet.FilterConfig; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 
import javax.servlet.annotation.WebFilter; 
 
/** 
 * 使用注解标注过滤器 
 * @WebFilter将一个实现了javax.servlet.Filter接口的类定义为过滤器 
 * 属性filterName声明过滤器的名称,可选 
 * 属性urlPatterns指定要过滤 的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性) 
 *  
 */ 
@WebFilter(filterName="myFilter",urlPatterns="/*") 
public class MyFilter implements Filter { 
 
    @Override 
    public void destroy() { 
 
    } 
 
    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
            throws IOException, ServletException { 
        request.setCharacterEncoding("UTF-8"); 
        response.setCharacterEncoding("UTF-8"); 
        response.setContentType("text/html;charset=utf-8"); 
        chain.doFilter(request, response); 
 
    } 
 
    @Override 
    public void init(FilterConfig arg0) throws ServletException { 
 
    } 
 
} 

方法二

application.properties添加

spring.http.encoding.charset=UTF-8 
spring.http.encoding.enabled=true 
spring.http.encoding.force=true

6.配置默认启动页 (MyWebAppConfigurer .java)

package cn.wuyang.springboot.config; 
 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
 
 
/** 
 * 1.拦截器:继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法。 
 * 2.自定义资源映射:继承 WebMvcConfigurerAdapter 并重写方法 addResourceHandlers方法  
 * 3.默认启动方法重写addViewControllers 
 * @author wuyang 
 * 
 */ 
@Configuration 
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter { 
 
    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
        registry.addViewController("/").setViewName("admin/index"); 
        super.addViewControllers(registry); 
    } 
} 

7.配置热启动pom.xml

首先集成jar包

<!-- 2.热启动 --> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-devtools</artifactId> 
            <optional>true</optional> 
        </dependency>

其次在

<build> 
        <plugins> 
            <plugin> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin</artifactId> 
                <dependencies> 
                    <!-- 2.热启动   添加-->  
                    <dependency> 
                        <groupId>org.springframework</groupId> 
                        <artifactId>springloaded</artifactId> 
                        <version>1.2.5.RELEASE</version> 
                    </dependency> 
                </dependencies> 
            </plugin> 
        </plugins> 
    </build>

8.spring boot banner设置(可选)

banner生成器
http://www.cnblogs.com/woshimrf/p/banner-ascii-2-txt.html

                                         _______  _        _______  
             |/     /||/     /||/     /|(  ___  )( (    /|(  ____ / 
             | )   ( || )   ( |( /   / )| (   ) ||  /  ( || (    // 
             | | _ | || |   | | / (_) / | (___) ||   / | || |       
             | |( )| || |   | |  /   /  |  ___  || (/ /) || | ____  
             | || || || |   | |   ) (   | (   ) || | /   || | /_  ) 
             | () () || (___) |   | |   | )   ( || )  /  || (___) | 
             (_______)(_______)   /_/   |/     /||/    )_)(_______)
${AnsiColor.BRIGHT_RED}:设置控制台中输出内容的颜色 
${application.version}:用来获取MANIFEST.MF文件中的版本号 
${application.formatted-version}:格式化后的${application.version}版本信息 
${spring-boot.version}:Spring Boot的版本号 
${spring-boot.formatted-version}:格式化后的${spring-boot.version}版本信息

9.引入版本号统一

首先在pom.xml 中添加依赖:

<dependency> 
    <groupId>org.webjars</groupId> 
    <artifactId>webjars-locator</artifactId> 
</dependency>

然后增加一个WebJarsController:

package cn.wuyang.springboot.controller.base; 
 
import javax.servlet.http.HttpServletRequest; 
 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.servlet.HandlerMapping; 
import org.webjars.WebJarAssetLocator; 
 
/** 
 * 处理WebJars,自动读取版本号 
 * 
 */ 
@Controller 
public class WebJarsController { 
 
     private final WebJarAssetLocator assetLocator = new WebJarAssetLocator(); 
 
    @ResponseBody 
    @RequestMapping("/webjarslocator/{webjar}/**") 
    public ResponseEntity<Object> locateWebjarAsset(@PathVariable String webjar, HttpServletRequest request) { 
        try { 
            String mvcPrefix = "/webjarslocator/" + webjar + "/"; // This prefix must match the mapping path! 
            String mvcPath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); 
            String fullPath = assetLocator.getFullPath(webjar, mvcPath.substring(mvcPrefix.length())); 
            return new ResponseEntity<>(new ClassPathResource(fullPath), HttpStatus.OK); 
        } catch (Exception e) { 
            return new ResponseEntity<>(HttpStatus.NOT_FOUND); 
        } 
    } 
}

10.资源名称md5方式

添加 application.properties 配置文件(或.yml)

spring.resources.chain.strategy.content.enabled=true 
spring.resources.chain.strategy.content.paths=/**

所有 /** 请求的静态资源都会被处理。
创建 ResourceUrlProviderController 文件

package cn.wuyang.springboot.controller.base; 
 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.ControllerAdvice; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.servlet.resource.ResourceUrlProvider; 
/** 
 * 处理静态资源URL 
 * 
 */ 
@ControllerAdvice 
public class ResourceUrlProviderController { 
 
    @Autowired 
    private ResourceUrlProvider resourceUrlProvider; 
 
    @ModelAttribute("urls") 
    public ResourceUrlProvider urls() { 
        return this.resourceUrlProvider; 
    } 
}

11.集成jquery

我们只需要在pom.xml 文件中添加jquery的webjars 依赖即可,如下

<dependency> 
    <groupId>org.webjars</groupId> 
    <artifactId>jquery</artifactId> 
    <version>3.1.0</version> 
</dependency>

未完待续。。。。。。。。

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/11255.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论