SpringMVC——HelloWorld
廢話不說。
首先,用IDEA新建一個maven專案。
如果是第一次用maven-archetype新建專案,可能需要等一段時間。我第一次就等了很久,睡了一覺以後,目錄結構如下圖:
新建了必要的資料夾後,目錄結構如下:
接下來就是用maven新增必要的jar包了,我的pom.xml檔案如下:(這個地方我參考了GW_Cheng的部落格http://blog.csdn.net/frankcheng5143/article/details/50512340)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jc_yoky</groupId>
<artifactId>springmvc</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>springmvc Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- basic settings -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.locales>zh_CN</project.build.locales>
<project.build.jdk>1.8</project.build.jdk>
<!-- lib versions -->
<junit.version>3.8.1</junit.version>
<spring.version>4.3.7.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>springmvc</finalName>
</build>
</project>
這樣,我們需要的jar包也匯入完畢了。
接下來,先部署到tomcat上看下是否報錯:
按照步驟來,不會有問題的!
啟動一下,應該是不會報錯,因為我們還什麼都沒幹……^ - ^
接下來就是springMVC的配置了,這才是重頭戲。
首先是web.xml檔案的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>springmvctest</display-name>
<!-- 統一編碼 -->
<filter>
<filter-name>charsetEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charsetEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置DispatchcerServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring mvc下的配置檔案的位置和名稱 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
然後在resources下新建springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<!-- 掃描路徑 -->
<context:component-scan base-package="com.hjc.springmvc" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置根檢視 -->
<mvc:view-controller path="/" view-name="index"/>
<!-- 啟用基於註解的配置 @RequestMapping, @ExceptionHandler,資料繫結 ,@NumberFormat ,
@DateTimeFormat ,@Controller ,@Valid ,@RequestBody ,@ResponseBody等 -->
<mvc:annotation-driven />
<!-- 靜態資源配置 -->
<mvc:resources location="/static/" mapping="/static/**"></mvc:resources>
<!-- 檢視層配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
這樣配置檔案也基本寫好了。此時的目錄結構如下:
接下來就是寫測試用的Java類了。在java/com/springmvc/controller下新建HelloWorld.java類:
package com.hjc.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Created by hjc on 2017/3/4.
*/
@Controller
@RequestMapping(value="/hello")
public class HelloWorld {
@RequestMapping(value="/world",method= RequestMethod.GET)
public String hello(Model model){
System.out.println("進入了hello方法");
model.addAttribute("msg","hello springmvc!");
return "index";
}
}
接下來是展示層index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h2>Hello Spring!</h2>
${msg}
<a href="/hello/world">顯示message</a>
</body>
</html>
執行tomcat,訪問localhost:8080,點選顯示message,“hello springmvc”就顯示出來了!
到這,springMVC框架的bootstrap就告一段落了。這其實就是一個非常簡單的檢視層到控制層,再返回檢視層的MVC架構了。
希望這個簡單的部落格能夠幫助到初學者,這裡也感謝【傑鍋鍋(Jackie)】、【GW_Cheng】兩位博主的賜教。
http://www.cnblogs.com/bigdataZJ/p/springmvc1.html
http://blog.csdn.net/frankcheng5143/article/details/50512340
有什麼問題歡迎留言,有錯誤的地方歡迎指正。
相關文章
- helloworld
- FASM之HelloWorldASM
- Servlet-HelloWorldServlet
- dubbo-HelloWorld
- Flutter系列(四)——HelloWorldFlutter
- 【SpringMVC】SpringMVC搭建框架SpringMVC框架
- Parcel 打包示例 – React HelloWorldReact
- redis整合springboot的helloworldRedisSpring Boot
- 彙編程式碼Helloworld
- 從 HelloWorld 說開去
- React入門-1.helloworldReact
- netty 之 telnet HelloWorld 詳解Netty
- 學習Source Generators之HelloWorld
- django新增路由訪問helloworldDjango路由
- Flask之 安裝與HelloWorldFlask
- RabbitMQ學習筆記-HelloWorldMQ筆記
- Netty5 入門HelloWorldNetty
- SpringMVC---IDEA 搭建SpringMVC工程SpringMVCIdea
- SpringMvc - SpringMvc的執行流程SpringMVC
- SpringMVCSpringMVC
- SpringMVC原始碼分析1:SpringMVC概述SpringMVC原始碼
- SpringMVC-08-SpringMVC層編寫SpringMVC
- SpringMVC-01 什麼是SpringMVCSpringMVC
- Go語言學習(2) - HelloWorldGo
- 機器語言編寫helloworld
- 用eclipes寫第一個HelloWorld
- grpc-go使用教程之helloworldRPCGo
- BitBake使用攻略--從HelloWorld講起
- Helloworld 驅動模組載入
- java基礎Day2 HelloWorldJava
- SpringMVC系列知識:(二)什麼是springMVC,為什麼要使用springMVCSpringMVC
- SpringMVC IOCSpringMVC
- SpringMVC 流程SpringMVC
- 遊戲小學生02-HelloWorld遊戲
- Docker安裝+HelloWorld+執行TomcatDockerTomcat
- Ubuntu 18.04.1 搭建Java環境和HelloWorldUbuntuJava
- 快速開始HelloWorld和Python之禪Python
- 13_Linux第一個程式HelloWorldLinux
- 入門Semantic Kernel:OneApi整合與HelloWorldAPI