1. 程式人生 > >SpringMVC基礎框架搭建

SpringMVC基礎框架搭建

utf del 參考 div username 信息 str adding 目錄

特別提示:本人博客部分有參考網絡其他博客,但均是本人親手編寫過並驗證通過。如發現博客有錯誤,請及時提出以免誤導其他人,謝謝!歡迎轉載,但記得標明文章出處:http://www.cnblogs.com/mao2080/   近些年來目前很多公司已經SpringMVC作為Struts的替代方案了,其優劣勢在此不做詳細講解了。而互聯網分布式系統的盛行使得單機web已經不再流行,更多的方向是動靜分離。動靜分離是將網站靜態資源(HTML,JavaScript,CSS,img等文件)與後臺應用(Java或其他後臺語言)分開部署。前後端使用異步方式(比如:ajax之類)進行數據交互。本文主要講解使用SpringMVC對外提供接口URL並返回json數據。 具體步驟如下:

1、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" 
version="3.0">

  <!--configure the setting of springmvcDispatcherServlet and configure the mapping
--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value
>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

2、spring-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-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                    

    <!-- 配置需要掃描的包 -->
    <context:component-scan base-package="com.mao2080.*"/>
    
    <!-- 配置啟用註解 -->
    <mvc:annotation-driven />
    
</beans>

3、pom.xml

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" 
 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4   <modelVersion>4.0.0</modelVersion>
 5   <groupId>mao.mao2080</groupId>
 6   <artifactId>springmvc</artifactId>
 7   <version>0.0.1-SNAPSHOT</version>
 8   <packaging>war</packaging>
 9   <dependencies>
10       <dependency>
11         <groupId>org.springframework</groupId>
12         <artifactId>spring-webmvc</artifactId>
13         <version>4.3.0.Release</version>
14     </dependency>
15     <dependency>
16         <groupId>javax.servlet</groupId>
17         <artifactId>javax.servlet-api</artifactId>
18         <version>4.0.0</version>
19         <scope>provided</scope>
20     </dependency>
21   </dependencies>
22 </project>

4、controller

 1 package com.mao2080.controller;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.ModelAttribute;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.ResponseBody;
 9 
10 import com.mao2080.beans.ResObject;
11 import com.mao2080.beans.User;
12 
13 @Controller
14 @RequestMapping("/testController")
15 public class TestController {
16     
17     /**
18      * 
19      * 描述:登錄
20      * @author Administrator
21      * @created 2017年12月2日 下午12:47:41
22      * @since 
23      * @param request
24      * @param o
25      * @return
26      */
27     @RequestMapping("/login")
28     @ResponseBody
29     public ResObject homeProduct(HttpServletRequest request, @ModelAttribute User o) {
30         if("admin".equals(o.getUserName()) && "admin".equals(o.getUserName())){
31             return new ResObject();
32         }
33         return new ResObject(403, "賬號或密碼錯誤...", null);
34     }
35 
36 }

5、ResObject

 1 package com.mao2080.beans;
 2 
 3 /**
 4  * 
 5  * 描述:返回POJO
 6  * @author Administrator
 7  * @created 2017年12月2日 下午12:46:20
 8  * @since
 9  */
10 public class ResObject {
11     
12     /**狀態碼*/
13     private int code = 200;
14     
15     /**描述信息(如果是異常則返回異常信息)*/
16     private String desc = "Success";
17     
18     /**成功後數據*/
19     private Object data;
20     
21     public ResObject() {
22         super();
23     }
24 
25     public ResObject(Object data) {
26         super();
27         this.data = data;
28     }
29 
30     public ResObject(int code, String desc, Object data) {
31         super();
32         this.code = code;
33         this.desc = desc;
34         this.data = data;
35     }
36 
37     public int getCode() {
38         return code;
39     }
40 
41     public void setCode(int code) {
42         this.code = code;
43     }
44 
45     public String getDesc() {
46         return desc;
47     }
48 
49     public void setDesc(String desc) {
50         this.desc = desc;
51     }
52 
53     public Object getData() {
54         return data;
55     }
56 
57     public void setData(Object data) {
58         this.data = data;
59     }
60 
61 }

6、User

 1 package com.mao2080.beans;
 2 
 3 /**
 4  * 
 5  * 描述:用戶pojo
 6  * @author Administrator
 7  * @created 2017年12月2日 下午12:44:43
 8  * @since
 9  */
10 public class User {
11     
12     private String userName;
13     
14     private String password;
15     
16     public User() {
17         super();
18     }
19 
20     public String getUserName() {
21         return userName;
22     }
23 
24     public void setUserName(String userName) {
25         this.userName = userName;
26     }
27 
28     public String getPassword() {
29         return password;
30     }
31 
32     public void setPassword(String password) {
33         this.password = password;
34     }
35 
36 }

7、訪問結果

技術分享圖片

技術分享圖片

8、目錄結構

技術分享圖片

9、資源下載

https://files.cnblogs.com/files/mao2080/springmvc.zip

SpringMVC基礎框架搭建