IDEA搭建SpringMVC簡單介面框架(Maven專案)
阿新 • • 發佈:2020-07-13
1, 新建專案,選擇Maven,如圖一次選擇,最後點選Next
2, 輸入GroupId和ArtifactId,點選Next
3,根據需要選擇自定義maven配置,點選Next。(①可以直接跳過)
4,根據需要修改專案名稱(一般不用修改),點選Finish。(①可以直接跳過)
5,當控制檯輸出BUILD SUCCESS後,專案建立成功。6,修改pom.xml,新增依賴包
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId><version>4.3.5.RELEASE</version>
</dependency>
</dependencies>
7,建立index.jsp【用於啟動專案專案時,預設展示頁面】 8,建立介面類(Controller)
package com.blueStarWei.controller; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/hello")
public class HelloWorld { @RequestMapping("/say/{name}")
public String say(@PathVariable String name){
return "Hello World, "+name;
}
}
9,配置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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.blueStarWei.*" /> <bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
</bean> </beans>
10,配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- DispatcherServlet預設使用WebApplicationContext作為上下文,
Spring預設配置檔案為“/WEB-INF/[servlet名字]-servlet.xml”
-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>
11,建立springmvc-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"> <context:component-scan base-package="com.blueStarWei.*" />
<mvc:annotation-driven/>
</beans>
12, 配置tomcat.(⑦和⑧可以跳過)
13,專案配置完成,可以啟動伺服器,(使用瀏覽器活postman等)進行訪問。