SpringMVC基础框架搭建

特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处: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

posted @ 2017-12-02 14:15  mao2080  阅读(206)  评论(0编辑  收藏  举报