Java 筆記 Spring(一)

來源:互聯網
上載者:User

標籤:oca   ann   Fix   建立   location   結構   web   筆記   使用   

一、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(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.