一、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(一)的更多相关文章

  1. Java笔记Spring(七)

    DispatcherServlet初始化,继续分析日志 主要部分: 23-May-2018 17:47:55.457 INFO [RMI TCP Connection(3)-127.0.0.1] or ...

  2. Java笔记Spring(五)

    C:\apache-tomcat-8.0.36\bin\catalina.bat run [2018-05-23 02:30:31,657] Artifact demo-springmvc:war e ...

  3. Java笔记Spring(四)

    spring web项目启动入口 1.首先看一下传统Java Web的配置文件web.xml,网上找的一个,参考地址:https://blog.csdn.net/github_36301064/art ...

  4. Java笔记Spring(八)

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  5. Java笔记Spring(六)

    web.xml各节点加载顺序 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=&q ...

  6. Java笔记Spring(三)

    spring-beans和spring-context 一.注解 1.自定义一个注解 @Target({ElementType.METHOD}) @Retention(RetentionPolicy. ...

  7. Java笔记Spring(二)

    spring-core 通过Gradle构建工具,转换包的命名空间为org.springframework下 cglib包,net.sf.cglib -> org.springframework ...

  8. Java笔记Spring(九)

    完整调试springmvc源码 WebApplicationContext = new XmlWebApplicationContext();// XmlWebApplicationContext通过 ...

  9. Java框架spring Boot学习笔记(六):Spring Boot事务管理

    SpringBoot和Java框架spring 学习笔记(十九):事务管理(注解管理)所讲的类似,使用@Transactional注解便可以轻松实现事务管理.

随机推荐

  1. lumion制作海上明月5.29

    点击海洋系统,打开海洋系统的开关,点击Q提升视线.按住shift提升海的高度,波浪强度调小.风速调高,风向调向朝向我们的方向,这样能看到更多涟漪. 拍照模式,添加特效,添加一个月亮,载增加一个太阳的特 ...

  2. sql中的limit关键字

    转载于:https://blog.csdn.net/benweizhu/article/details/7892788 一.基本 SQL的limit语法的如以下形式 SELECT * FROM tab ...

  3. ERROR in Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (64)

    该问题说的是当前环境不支持node-sass,网上说了一下是要安装node 7一下版本才支持. 这里改使用less-loader,及less

  4. UVa Live 4794 - Sharing Chocolate 枚举子集substa = (s - 1) & substa,记忆化搜索 难度: 2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  5. 用Python自动发送邮件

    用Python自动发送邮件     最近需要在服务器上处理一些耗时比较长的任务,因此想到利用python写一个自动发送邮件的脚本,在任务执行完毕后发送邮件通知我.以下代码以163邮箱为例: 开通163 ...

  6. css实现半圆和圆

    1.半圆 半圆分为:上半圆.下半圆.左半圆.右半圆,我们只要学会制作某一个方向的半圆,其他方向的半圆都可以轻松实现,其原理都一样. 假如我们要制作上半圆,实现原理:把高度height设置为宽度widt ...

  7. js计算两个日期相差天数

    //两个时间相差天数 兼容firefox chrome var days = function(startDate) { var sdate = new Date(startDate.replace( ...

  8. opcode cache与JIT的区别

    要说明opcode cache与JIT的区别,得先明白,字节码,又叫中间码与机器码的区别. 操作码(opcode) 一条机器指令.比如我们汇编语言写的一条操作语句. 机器码(machine code) ...

  9. jsonp 实现原理

      Jsonp原理: 首先在客户端注册一个callback, 然后把callback的名字传给服务器. 此时,服务器先生成 json 数据.然后以 javascript 语法的方式,生成一个funct ...

  10. disjoint set

    MAKE-SET.x/ creates a new set whose only member (and thus representative) is x. Since the sets are d ...