• 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,显示正常。

  • 相关阅读:
    新人手册
    使用koa-mysql-session时报错
    自建windows服务器如何部署egg应用
    mac os 10.15 virtualBox6.0.12崩溃
    thinkphp3.2 上传图片兼容小程序
    PHP版本微信支付开发
    php mysql 按照指定年月查找数据 数据库create_time为时间戳
    Mac OS 查看端口和杀死进程
    MAC PHP7 如何disable xdebug
    TCP为什么是三次握手,为什么不是两次或者四次 && TCP四次挥手
  • 原文地址:https://www.cnblogs.com/tq1226112215/p/9057115.html
Copyright © 2020-2023  润新知