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注解便可以轻松实现事务管理.
随机推荐
- 点击事件target
1.场景:需要写一个弹出框来容纳登录界面,类似于百度的登录框 问题:使用 display: none/block 进行弹出框的显示和隐藏,设立点击事件 onclick 进行触发,但是点击 ...
- java使用StringBuilder的方法反转字符串输出
public class FanZhuan { public static void main(String[] args) { /**第一种方法*/ String s = "9876543 ...
- 二十二. Python基础(22)--继承
二十二. Python基础(22)--继承 ● 知识框架 ● 继承关系中self的指向 当一个对象调用一个方法时,这个方法的self形参会指向这个对象 class A: def get(s ...
- 软工作业No.7 甜美女孩第五周--测试与发布
Alpha版本发布说明 一.功能介绍 本团队所做的多模式自定义2048是用来进行纸牌模式以及正常基础模式版本的2048小游戏的.Alpha版本具有的功能大体如下: 初始界面- 纸牌模式- 基础模式- ...
- linux curl http get 请求中带有中文参数或者特殊字符处理
在使用c++去请求http服务的时候,使用的是著名的curl工具提供的类库 libcurl,但是在使用的过程中发现,如果请求的参数值带了空格或者是参数是中文,会导致响应的回调函数没有被执行,虽然cur ...
- json:java中前台向后台传对象数据
前台传入的是一个json类型的数据,如何在后台解析成想要的数据类型? 例如: 后台获取了前台一个string类型的数据@RequestParam(value = "forceUpgradeT ...
- python变量进阶(可变不可变,局部变量和全局变量)
变量进阶(理解) 目标 变量的引用 可变和不可变类型 局部变量和全局变量 01. 变量的引用 变量 和 数据 都是保存在 内存 中的 在 Python 中 函数 的 参数传递 以及 返回值 都是靠 引 ...
- Oracle 创建存储过程 提示权限不足或者提示表和视图不存在问题
grant create view to hospital; --授予查询权限 grant select any table to hospital; --授予权限 grant select any ...
- java面向对象编程(二)-构造方法(函数)
1.类的构造方法介绍 什么是构造方法呢?在回答这个问题之前,我们来看一个需求:我们在创建人类的对象时,是先把一个对象创建好后,再给他的年龄和姓名属性赋值,如果现在我要求,在创建人类的对象时,就直接指定 ...
- 如何用Caffe训练自己的网络-探索与试验
现在一直都是用Caffe在跑别人写好的网络,如何运行自定义的网络和图片,是接下来要学习的一点. 1. 使用Caffe中自带的网络模型来运行自己的数据集 参考 [1] :http://www.cnblo ...