Application.properties 中
#指定端口号 server.port=8087 #指定访问路径必须以/crud/xxx 开始 server.servlet.context-path=/crud #指定编码格式 server.tomcat.uri-encoding=UTF-8 #指定日期格式 spring.mvc.date-format=yyyy-MM-dd # 禁用缓存 spring.thymeleaf.cache=false # 国际化配置文件(包名.基础名) spring.messages.basename=i18n.login
pom 文件中需要加上自己需要的依赖:如
<!--引入jquery-webjar--> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1</version> </dependency> <!--引入bootstrap--> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>4.0.0</version> </dependency>
静态页面不要放在静态文件夹下,会得不到模板引擎的解析需要放在
首先就是要访问我们的首页,这是可以指定的首先可以写一个方法这时有两种方法
(1)不推荐 在controller中写上一个方法
@RequestMapping({"/","/index.html"}) public String index(){ //模板引擎会自动进行拼写自动加上.html return "login"; }
(2)加上视图映射 viewControlle
手动写上一个配置类使用视图映射
因为WebMvcConfigurerAdapter 在Spring5.0已被废弃所以 博主推荐的两种方式https://blog.csdn.net/lenkvin/article/details/79482205
1 直接实现WebMvcConfigurer (官方推荐)
@Configuration public class WebMvcConfg implements WebMvcConfigurer { //todo }
2 直接继承WebMvcConfigurationSupport
@Configuration public class WebMvcConfg extends WebMvcConfigurationSupport { //todo }
使用第一种方法
说下默认映射的文件夹有:
classpath:/META-INF/resources
-
classpath:/resources
-
classpath:/static
-
classpath:/public
上面这几个都是静态资源的映射路径,优先级顺序为:META-INF/resources > resources > static > public
我们可以通过修改spring.mvc.static-path-pattern来修改默认的映射**
具体信息https://www.cnblogs.com/java-synchronized/p/7091723.html
对login.html 页面的修改
th:text是对标签体中的修改
th:text="#{login.password}
而复选框是字节数的没有标签体 所以使用
[[#{login.remember}]]
注册国际化标准i18N
别忘了在配置文件中加上
# 国际化配置文件(包名.基础名)
spring.messages.basename=i18n.login
放行静态资源
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/login.html","/","/webjars/**","/user/login","/static/**");
=====================================================================================================================================
springboot的基础入门
libraries 库(maven自定下载的jar包)
compiler.xml 编译器(主要用于maven初始化的编译)
misc.xml(混乱的,多种多样的)
modules.xml(模块的xml)
thriftCompiler.xml(节约,节俭)
workspace.xml
<?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="ChangeListManager"> <list default="true" id="1205bb8f-51e7-4e4d-bf0c-80e4ab858e97" name="Default Changelist" comment="" /> <ignored path="$PROJECT_DIR$/out/" /> <ignored path="$PROJECT_DIR$/target/" /> <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> <option name="SHOW_DIALOG" value="false" /> <option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="LAST_RESOLUTION" value="IGNORE" /> </component> <component name="FUSProjectUsageTrigger"> <session id="1908026401"> <usages-collector id="statistics.lifecycle.project"> <counts> <entry key="project.closed" value="1" /> <entry key="project.open.time.1" value="1" /> <entry key="project.open.time.6" value="1" /> <entry key="project.opened" value="2" /> </counts> </usages-collector> <usages-collector id="statistics.file.types.open"> <counts> <entry key="IDEA_MODULE" value="2" /> <entry key="JAVA" value="1" /> <entry key="Markdown" value="1" /> <entry key="PLAIN_TEXT" value="3" /> <entry key="Properties" value="1" /> <entry key="XML" value="11" /> </counts> </usages-collector> </session> </component> <component name="FileEditorManager"> <leaf> <file pinned="false" current-in-tab="false"> <entry file="file://$PROJECT_DIR$/.idea/libraries/Maven__ch_qos_logback_logback_classic_1_2_3.xml"> <provider selected="true" editor-type-id="text-editor" /> </entry> </file> <file pinned="false" current-in-tab="false"> <entry file="file://$PROJECT_DIR$/.idea/compiler.xml"> <provider selected="true" editor-type-id="text-editor"> <state relative-caret-position="306"> <caret line="17" column="26" selection-start-line="17" selection-start-column="26" selection-end-line="17" selection-end-column="26" /> </state> </provider> </entry> </file> <file pinned="false" current-in-tab="true"> <entry file="file://$PROJECT_DIR$/.idea/misc.xml"> <provider selected="true" editor-type-id="text-editor"> <state relative-caret-position="216"> <caret line="12" column="10" lean-forward="true" selection-start-line="12" selection-start-column="10" selection-end-line="12" selection-end-column="10" /> </state> </provider> </entry> </file> </leaf> </component> <component name="JsBuildToolGruntFileManager" detection-done="true" sorting="DEFINITION_ORDER" /> <component name="JsBuildToolPackageJson" detection-done="true" sorting="DEFINITION_ORDER" /> <component name="JsGulpfileManager"> <detection-done>true</detection-done> <sorting>DEFINITION_ORDER</sorting> </component> <component name="ProjectFrameBounds" extendedState="6"> <option name="x" value="-158" /> <option name="y" value="50" /> <option name="width" value="729" /> <option name="height" value="783" /> </component> <component name="ProjectView"> <navigator proportions="" version="1"> <foldersAlwaysOnTop value="true" /> </navigator> <panes> <pane id="Scope" /> <pane id="PackagesPane" /> <pane id="ProjectPane"> <subPane> <expand> <path> <item name="01-example" type="b2602c69:ProjectViewProjectNode" /> <item name="01-example" type="462c0819:PsiDirectoryNode" /> </path> <path> <item name="01-example" type="b2602c69:ProjectViewProjectNode" /> <item name="01-example" type="462c0819:PsiDirectoryNode" /> <item name=".idea" type="462c0819:PsiDirectoryNode" /> </path> <path> <item name="01-example" type="b2602c69:ProjectViewProjectNode" /> <item name="01-example" type="462c0819:PsiDirectoryNode" /> <item name=".mvn" type="462c0819:PsiDirectoryNode" /> </path> <path> <item name="01-example" type="b2602c69:ProjectViewProjectNode" /> <item name="01-example" type="462c0819:PsiDirectoryNode" /> <item name=".mvn" type="462c0819:PsiDirectoryNode" /> <item name="wrapper" type="462c0819:PsiDirectoryNode" /> </path> </expand> <select /> </subPane> </pane> </panes> </component> <component name="PropertiesComponent"> <property name="WebServerToolWindowFactoryState" value="false" /> <property name="android.sdk.path" value="D:/SDK" /> <property name="aspect.path.notification.shown" value="true" /> <property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1556162342378" /> <property name="last_opened_file_path" value="$PROJECT_DIR$" /> <property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" /> <property name="nodejs_npm_path_reset_for_default_project" value="true" /> <property name="settings.editor.selected.configurable" value="File.Encoding" /> </component> <component name="RunDashboard"> <option name="ruleStates"> <list> <RuleState> <option name="name" value="ConfigurationTypeDashboardGroupingRule" /> </RuleState> <RuleState> <option name="name" value="StatusDashboardGroupingRule" /> </RuleState> </list> </option> </component> <component name="RunManager"> <configuration name="Application" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot"> <module name="01-example" /> <option name="SPRING_BOOT_MAIN_CLASS" value="com.example.Application" /> <method v="2"> <option name="Make" enabled="true" /> </method> </configuration> </component> <component name="SvnConfiguration"> <configuration /> </component> <component name="TaskManager"> <task active="true" id="Default" summary="Default task"> <changelist id="1205bb8f-51e7-4e4d-bf0c-80e4ab858e97" name="Default Changelist" comment="" /> <created>1552830985946</created> <option name="number" value="Default" /> <option name="presentableId" value="Default" /> <updated>1552830985946</updated> <workItem from="1552830990066" duration="127000" /> <workItem from="1556158361283" duration="3805000" /> </task> <servers /> </component> <component name="TimeTrackingManager"> <option name="totallyTimeSpent" value="3932000" /> </component> <component name="ToolWindowManager"> <frame x="-8" y="-8" width="1382" height="744" extended-state="6" /> <editor active="true" /> <layout> <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.21331316" /> <window_info id="Structure" order="1" side_tool="true" weight="0.25" /> <window_info id="Designer" order="2" /> <window_info id="JRebel" order="3" side_tool="true" /> <window_info id="Image Layers" order="4" /> <window_info id="UI Designer" order="5" /> <window_info id="Favorites" order="6" side_tool="true" /> <window_info id="Capture Tool" order="7" /> <window_info id="Web" order="8" side_tool="true" /> <window_info anchor="bottom" id="Message" order="0" /> <window_info anchor="bottom" id="Find" order="1" /> <window_info anchor="bottom" id="Run" order="2" /> <window_info anchor="bottom" id="Debug" order="3" weight="0.4" /> <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" /> <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" /> <window_info anchor="bottom" id="TODO" order="6" /> <window_info anchor="bottom" id="Spring" order="7" /> <window_info anchor="bottom" id="Terminal" order="8" /> <window_info anchor="bottom" id="Event Log" order="9" side_tool="true" /> <window_info anchor="bottom" id="Java Enterprise" order="10" /> <window_info anchor="bottom" id="Database Changes" order="11" show_stripe_button="false" /> <window_info anchor="bottom" id="Version Control" order="12" show_stripe_button="false" /> <window_info anchor="bottom" id="JRebel Console" order="13" side_tool="true" /> <window_info anchor="right" id="SciView" /> <window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" /> <window_info anchor="right" id="Ant Build" order="1" weight="0.25" /> <window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" /> <window_info anchor="right" id="Palette" order="3" /> <window_info anchor="right" id="Capture Analysis" order="4" /> <window_info anchor="right" id="Maven Projects" order="5" /> <window_info anchor="right" id="Database" order="6" /> <window_info anchor="right" id="Palette	" order="7" /> <window_info anchor="right" id="Theme Preview" order="8" /> <window_info anchor="right" id="Redis Servers" order="9" /> <window_info anchor="right" id="Bean Validation" order="10" /> </layout> </component> <component name="TypeScriptGeneratedFilesManager"> <option name="version" value="1" /> </component> <component name="VcsContentAnnotationSettings"> <option name="myLimit" value="2678400000" /> </component> <component name="editorHistoryManager"> <entry file="file://$PROJECT_DIR$/.idea/libraries/Maven__org_springframework_spring_context_5_1_5_RELEASE.xml"> <provider selected="true" editor-type-id="text-editor" /> </entry> <entry file="file://$PROJECT_DIR$/.idea/modules.xml"> <provider selected="true" editor-type-id="text-editor" /> </entry> <entry file="file://$PROJECT_DIR$/.idea/thriftCompiler.xml"> <provider selected="true" editor-type-id="text-editor"> <state relative-caret-position="90"> <caret line="5" column="10" lean-forward="true" selection-start-line="5" selection-start-column="10" selection-end-line="5" selection-end-column="10" /> </state> </provider> </entry> <entry file="file://$PROJECT_DIR$/.mvn/wrapper/MavenWrapperDownloader.java"> <provider selected="true" editor-type-id="text-editor"> <state relative-caret-position="-1018"> <caret line="33" column="29" selection-start-line="33" selection-start-column="25" selection-end-line="33" selection-end-column="29" /> <folding> <element signature="e#0#4628#0" expanded="true" /> </folding> </state> </provider> </entry> <entry file="file://$PROJECT_DIR$/.mvn/wrapper/maven-wrapper.properties"> <provider selected="true" editor-type-id="text-editor"> <state relative-caret-position="18"> <caret line="1" selection-start-line="1" selection-end-line="1" /> </state> </provider> </entry> <entry file="file://$PROJECT_DIR$/.idea/encodings.xml"> <provider selected="true" editor-type-id="text-editor"> <state relative-caret-position="36"> <caret line="2" column="17" selection-start-line="2" selection-start-column="17" selection-end-line="2" selection-end-column="17" /> </state> </provider> </entry> <entry file="file://$PROJECT_DIR$/mvnw.cmd"> <provider selected="true" editor-type-id="text-editor" /> </entry> <entry file="file://$PROJECT_DIR$/.gitignore"> <provider selected="true" editor-type-id="text-editor"> <state relative-caret-position="-45" /> </provider> </entry> <entry file="file://$PROJECT_DIR$/mvnw"> <provider selected="true" editor-type-id="text-editor"> <state relative-caret-position="-3024" /> </provider> </entry> <entry file="file://$PROJECT_DIR$/HELP.md"> <provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]"> <state split_layout="SPLIT"> <first_editor relative-caret-position="90"> <caret line="5" column="56" lean-forward="true" selection-start-line="5" selection-start-column="53" selection-end-line="5" selection-end-column="56" /> </first_editor> <second_editor /> </state> </provider> </entry> <entry file="file://$PROJECT_DIR$/01-example.iml"> <provider selected="true" editor-type-id="text-editor" /> </entry> <entry file="file://$PROJECT_DIR$/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_9_8.xml"> <provider selected="true" editor-type-id="text-editor" /> </entry> <entry file="file://$PROJECT_DIR$/.idea/libraries/Maven__ch_qos_logback_logback_classic_1_2_3.xml"> <provider selected="true" editor-type-id="text-editor" /> </entry> <entry file="file://$PROJECT_DIR$/.idea/compiler.xml"> <provider selected="true" editor-type-id="text-editor"> <state relative-caret-position="306"> <caret line="17" column="26" selection-start-line="17" selection-start-column="26" selection-end-line="17" selection-end-column="26" /> </state> </provider> </entry> <entry file="file://$PROJECT_DIR$/.idea/misc.xml"> <provider selected="true" editor-type-id="text-editor"> <state relative-caret-position="216"> <caret line="12" column="10" lean-forward="true" selection-start-line="12" selection-start-column="10" selection-end-line="12" selection-end-column="10" /> </state> </provider> </entry> </component> <component name="masterDetails"> <states> <state key="ProjectJDKs.UI"> <settings> <last-edited>1.8</last-edited> <splitter-proportions> <option name="proportions"> <list> <option value="0.2" /> </list> </option> </splitter-proportions> </settings> </state> </states> </component> </project>
下面介绍.mvn
springboot中的.mvn主要用于版本切换因为maven的版本和插件配合并不是那么完美,.mvn为解决这个问题有一个第三方的插件wrapper可以使用
properties文件中记录你要使用的maven版本当用户执行mvn的clear命令时发现当前用户使用的maven版本和用户期望的版本不一样那么就下载期望的maven版本。
.gitignore 的作用 用于和git官网
demo.iml idea工程的配置文件是当前project的一些配置信息
4.yml文件中的值不能以*开头,可以用 "*" 加个引号开头。
接下来解析配置文件
pom文件
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
它的父项目是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
他来真正管理Spring Boot应用里面的所有依赖版本;
Spring Boot的版本仲裁中心;
以后我们导入依赖默认是不需要写版本;(没有在dependencies里面管理的依赖自然需要声明版本号)
简化部署
<!‐‐ 这个插件,可以将应用打包成一个可执行的jar包;‐‐> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring‐boot‐maven‐plugin</artifactId> </plugin> </plugins> </build>
将这个应用打成jar包,直接使用java -jar的命令进行执行;
//@SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
@SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用。
2、启动器spring-boot-starter-web:spring-boot-starter:spring-boot场景启动器;
帮我们导入了web模块正常运行所依赖的组件;Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),
只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。
要用什么功能就导入什么场景的启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
</dependency>12
springboot中配置文件主启动类配置中的注解
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication {
@SpringBootConfiguration:Spring Boot的配置类;标注在某个类上,表示这是一个Spring Boot的配置类;
@Configuration:配置类上来标注这个注解;配置类 ----- 配置文件;配置类也是容器中的一个组件;@Component
@EnableAutoConfiguration:开启自动配置功能;以前我们需要配置的东西,Spring Boot帮我们自动配置;
@EnableAutoConfiguration告诉SpringBoot开启自动配置功能;这样自动配置才能生
EnableAutoConfiguration 中的注解
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration {