Java笔记Spring(一)
一、Spring框架
源码地址:https://github.com/spring-projects/spring-framework
构建工具:Gradle,Gradle教程:https://www.w3cschool.cn/gradle/
Gradle基于Groovy语言,Groovy教程:https://www.w3cschool.cn/groovy/

JSR标准相关的资料: https://jcp.org/en/jsr/all
二、Spring框架Module
官网文档:https://docs.spring.io/spring/docs/4.3.17.RELEASE/spring-framework-reference/htmlsingle/#overview-modules

三、使用Maven构建demo-springmvc项目
File -> New Project -> Maven(勾选Create from archetype,同时选择maven-archetype-webapp) -> Next
GroupId:com.example
ArtifactId:demo-spring
-> Next -> Next ->
Project name:demo-spring
-> Finish


对比上面两图,发现不同点只在于有没有web结构,而且默认的web结构还是需要修改的。 继续...
添加spring mvc框架支持,直接在pom.xml文件中dependencies下添加spring mvc的依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
替换web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
WEB-INF下添加applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
dispatcher-servlet.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--spring基本注解支持 -->
<context:annotation-config/> <!--mvc注解支持-->
<mvc:annotation-driven/> <!--静态资源映射-->
<mvc:resources mapping="/css/**" location="/WEB-INF/statics/css/"/>
<mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/"/>
<mvc:resources mapping="/image/**" location="/WEB-INF/statics/image/"/> <!--jsp模板视图支持-->
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
</bean> <!--自动扫描装配-->
<context:component-scan base-package="com.example.demo"/>
</beans>
写个Controller跑起来看看
在 src -> main 下建两个文件夹,java和test,创建完成后,分别设置资源类型

java文件夹设置为 sources,test文件夹设置为 tests
创建一个测试controller

配置测试服务器




点击启动,访问 localhost:8080/user/get
会显示 ??
原因:spring mvc默认输出字符集 iso-8859-1,需要将输出字符集调整为 utf-8

再次启动,访问 localhost:8080/user/get,显示正常。
Java笔记Spring(一)的更多相关文章
- Java笔记Spring(七)
DispatcherServlet初始化,继续分析日志 主要部分: 23-May-2018 17:47:55.457 INFO [RMI TCP Connection(3)-127.0.0.1] or ...
- Java笔记Spring(五)
C:\apache-tomcat-8.0.36\bin\catalina.bat run [2018-05-23 02:30:31,657] Artifact demo-springmvc:war e ...
- Java笔记Spring(四)
spring web项目启动入口 1.首先看一下传统Java Web的配置文件web.xml,网上找的一个,参考地址:https://blog.csdn.net/github_36301064/art ...
- Java笔记Spring(八)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Java笔记Spring(六)
web.xml各节点加载顺序 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=&q ...
- Java笔记Spring(三)
spring-beans和spring-context 一.注解 1.自定义一个注解 @Target({ElementType.METHOD}) @Retention(RetentionPolicy. ...
- Java笔记Spring(二)
spring-core 通过Gradle构建工具,转换包的命名空间为org.springframework下 cglib包,net.sf.cglib -> org.springframework ...
- Java笔记Spring(九)
完整调试springmvc源码 WebApplicationContext = new XmlWebApplicationContext();// XmlWebApplicationContext通过 ...
- Java框架spring Boot学习笔记(六):Spring Boot事务管理
SpringBoot和Java框架spring 学习笔记(十九):事务管理(注解管理)所讲的类似,使用@Transactional注解便可以轻松实现事务管理.
随机推荐
- Vue(六) 表单与 v-model
学习使用 v-model 指令完成表单数据双向绑定 基本用法 表单控件在实际业务较为常见,比如单选,多选.下拉选择.输入框等,用他们可以完成数据的录入.校验.提交等.Vue.js 提供了 v-mode ...
- 31 位域、空类的sizeof值
1 分析下列程序: #include<iostream> using namespace std; struct s { int x: 3; int y: 4; int z: 5; dou ...
- unity 中的UGUI 屏蔽鼠标穿透
void Update() { if(IsTouchedUI()) { Debug.Log("当前触摸在UI上"); } else { Debug.Log("当前没有触摸 ...
- tensorFlow 零散知识
收集一些碰到的关于细节的函数在这里记录下 1.tf.flags.DEFINE_xxx() 读别人家的代码的时候经常看到这个,结果两三天不看居然忘记了,这脑子绝对上锈了,决定记下来免得老是查来查去的.. ...
- JavaScript 之 BOM
BOM BOM(Bowser Object Model) 浏览器对象模型 提供了独立于页面内容而与浏览器就行交互的对象,核心对象是window. (BOM 无标准支持) Navigator 浏览器 ...
- Linux性能监控分析命令(一)—vmstat命令详解
一.vmstat介绍 语法格式: vmstat [-V] [-n] [-S unit] [delay [count]] -V prints version. -n causes the headers ...
- asp.net button控件 使用JS的 disabled
今天想用JS禁用asp.net的button控件,查了好久,都是一行代码.... document.getElementById("Button1").disabled ...
- Redis学习第六课:Redis ZSet类型及操作
Sorted set是set的一个升级版本,它在set的基础上增加了一个顺序属性,这一属性在添加修改元素时候可以指定,每次指定后,zset会自动重新按新的值调整顺序.可以理解为有两列字段的数据表,一列 ...
- ProtocolBuffer for Objective-C 运行环境配置及使用
1,我已经安装了brew.pod.protoc,如果您没安装,请按照下面方式安装. 安装很简单,对着README操作一遍即可,我贴出自己在终端的命令行.需要输入的命令行依次为:1)打开终端,查看mac ...
- 基于SPA的网页授权流程(微信OAuth2)
先说传统MVC网站的网页授权流程. 1.用户发起了某个需要登录执行的操作 2.收集AppId等信息重定向到微信服务器 3.微信服务器回调到网站某个Controller的Action 4.在此Actio ...