zoukankan      html  css  js  c++  java
  • 使用Spring MVC实现RESTful接口

    [java] view plain copy
     
    1. <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">   </span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">作为编程界新人,这两天接到老大的指示,先去拿Spring MVC实现RESTful接口,熟悉一下以前的技术。这个时候脑袋好大,java都忘得差不多了,更何况用Spring搭建MVC。没办法只能用度娘来帮忙了,可是网上各种说法,各种迷糊,终于找到了一篇比较适合新手的博文,再此表示感谢。</span>  

    下文为转载:

    下面我把我用到的相关软件版本列一下,虽然我不知道这些有什么关系,但是,还是列一下吧,

    因为不知道列哪些,所以就把这些都截了个图害羞,只知道要用jdk,maven,tomcat,和Java编译器(如eclipse)

    现在开始建项目

    1、创建一个maven项目

     

    图1

     

    图2

     

    图3(第一个复选框一定要框,反正我这里不选就是错~)

     

    图4(Packaging选择war。反正我之前选jar就有问题了)

     



    图5(红框中显示的不是我想要的,需要修改,右击它,选择“Build Path” ==>“ConfigureBuild Path”)


    图6

    图7(不知道为什么选这个,只是之前看同事选了这个。)

    图8

    图9

    到这一步,已经算是建好了一个mave项目。我们现在来配置一些东西。

    2、配置pom.xml(可以直接添加到pom.xml里面,配置一些七七八八的东西。junit、jstl、 spring包)

    [html] view plain copy
     
    1. <dependencies>    
    2.         <!-- junit -->    
    3.         <dependency>    
    4.             <groupId>junit</groupId>    
    5.             <artifactId>junit</artifactId>    
    6.             <version>4.12-beta-3</version>    
    7.             <scope>test</scope>    
    8.         </dependency>    
    9.         <!-- log4j -->    
    10.         <dependency>    
    11.             <groupId>log4j</groupId>    
    12.             <artifactId>log4j</artifactId>    
    13.             <version>1.2.17</version>    
    14.         </dependency>    
    15.             
    16.         <dependency>    
    17.             <groupId>jstl</groupId>    
    18.             <artifactId>jstl</artifactId>    
    19.             <version>1.2</version>    
    20.         </dependency>    
    21.             
    22.         <!-- spring 的基本依赖 开始 -->    
    23.     
    24.         <dependency>    
    25.             <groupId>org.springframework</groupId>    
    26.             <artifactId>spring-core</artifactId>    
    27.             <version>4.1.2.RELEASE</version>    
    28.         </dependency>    
    29.     
    30.         <dependency>    
    31.             <groupId>org.springframework</groupId>    
    32.             <artifactId>spring-expression</artifactId>    
    33.             <version>4.1.2.RELEASE</version>    
    34.         </dependency>    
    35.     
    36.         <dependency>    
    37.             <groupId>org.springframework</groupId>    
    38.             <artifactId>spring-beans</artifactId>    
    39.             <version>4.1.2.RELEASE</version>    
    40.         </dependency>    
    41.     
    42.         <dependency>    
    43.             <groupId>org.springframework</groupId>    
    44.             <artifactId>spring-aop</artifactId>    
    45.             <version>4.1.2.RELEASE</version>    
    46.         </dependency>    
    47.     
    48.         <dependency>    
    49.             <groupId>org.springframework</groupId>    
    50.             <artifactId>spring-context</artifactId>    
    51.             <version>4.1.2.RELEASE</version>    
    52.         </dependency>    
    53.         <dependency>    
    54.             <groupId>org.springframework</groupId>    
    55.             <artifactId>spring-context-support</artifactId>    
    56.             <version>4.1.2.RELEASE</version>    
    57.         </dependency>    
    58.     
    59.         <dependency>    
    60.             <groupId>org.springframework</groupId>    
    61.             <artifactId>spring-tx</artifactId>    
    62.             <version>4.1.2.RELEASE</version>    
    63.         </dependency>    
    64.     
    65.         <dependency>    
    66.             <groupId>org.springframework</groupId>    
    67.             <artifactId>spring-web</artifactId>    
    68.             <version>4.1.2.RELEASE</version>    
    69.         </dependency>    
    70.         <dependency>    
    71.             <groupId>org.springframework</groupId>    
    72.             <artifactId>spring-jdbc</artifactId>    
    73.             <version>4.1.2.RELEASE</version>    
    74.         </dependency>    
    75.     
    76.         <dependency>    
    77.             <groupId>org.springframework</groupId>    
    78.             <artifactId>spring-webmvc</artifactId>    
    79.             <version>4.1.2.RELEASE</version>    
    80.         </dependency>    
    81.         <dependency>    
    82.             <groupId>org.springframework</groupId>    
    83.             <artifactId>spring-aspects</artifactId>    
    84.             <version>4.1.2.RELEASE</version>    
    85.         </dependency>    
    86.     
    87.         <dependency>    
    88.             <groupId>org.springframework</groupId>    
    89.             <artifactId>spring-test</artifactId>    
    90.             <version>4.1.2.RELEASE</version>    
    91.         </dependency>    
    92.         <!-- spring 的基本依赖 结束 -->    
    93.   </dependencies>    

    3、在webapp下添加一个文件夹“WEB-INF”,在文件夹下添加一个文件“web.xml”(下面的内容可以全部复制过去)

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>    
    2. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    3.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    
    4.     version="2.5">    
    5.     <!-- 区分项目名称,防止默认重名 -->      
    6.     <context-param>      
    7.         <param-name>webAppRootKey</param-name>      
    8.         <param-value>maven.example.root</param-value>      
    9.     </context-param>      
    10.       
    11.     <!-- Spring的log4j监听器 -->      
    12.     <listener>      
    13.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>      
    14.     </listener>      
    15.       
    16.     <!-- 字符集 过滤器  -->      
    17.     <filter>      
    18.         <filter-name>CharacterEncodingFilter</filter-name>      
    19.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>      
    20.         <init-param>      
    21.             <param-name>encoding</param-name>      
    22.             <param-value>UTF-8</param-value>      
    23.         </init-param>      
    24.         <init-param>      
    25.             <param-name>forceEncoding</param-name>      
    26.             <param-value>true</param-value>      
    27.         </init-param>      
    28.     </filter>      
    29.     <filter-mapping>      
    30.         <filter-name>CharacterEncodingFilter</filter-name>      
    31.         <url-pattern>/*</url-pattern>      
    32.     </filter-mapping>      
    33.       
    34.     <!-- Spring view分发器 -->      
    35.     <servlet>      
    36.         <servlet-name>dispatcher0121</servlet-name>      
    37.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      
    38.         <init-param>      
    39.             <param-name>contextConfigLocation</param-name>      
    40.             <param-value>/WEB-INF/dispatcher0121-servlet.xml</param-value>      
    41.         </init-param>      
    42.         <load-on-startup>1</load-on-startup>      
    43.     </servlet>      
    44.     <servlet-mapping>      
    45.         <servlet-name>dispatcher0121</servlet-name>      
    46.         <url-pattern>*.do</url-pattern>      
    47.     </servlet-mapping>      
    48.     
    49. </web-app>    

    web.xml文件里有几个值得注意的地方,上面标红的“dispatcher0121”可以修改为你想要的字符串,但是这个三个地方一定要一样。[servlet-name]+“-servlet.xml”这个是spring配置文件的文件名的命名规则。

    4、在“webapp/WEB-INF/”下再添加一个文件“dispatcher0121-servlet.xml”,这个文件就是上面说的东西(可以全部复制到新建的文件中)

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>      
    2. <beans xmlns="http://www.springframework.org/schema/beans"       
    3.        xmlns:aop="http://www.springframework.org/schema/aop"       
    4.        xmlns:context="http://www.springframework.org/schema/context"      
    5.        xmlns:mvc="http://www.springframework.org/schema/mvc"       
    6.        xmlns:tx="http://www.springframework.org/schema/tx"       
    7.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
    8.        xsi:schemaLocation="http://www.springframework.org/schema/aop       
    9.         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd       
    10.         http://www.springframework.org/schema/beans       
    11.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       
    12.         http://www.springframework.org/schema/context       
    13.         http://www.springframework.org/schema/context/spring-context-3.0.xsd       
    14.         http://www.springframework.org/schema/mvc       
    15.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd       
    16.         http://www.springframework.org/schema/tx       
    17.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">      
    18.       
    19.     <mvc:annotation-driven />      
    20.     <context:component-scan base-package="example0121" />      
    21.       
    22.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">      
    23.         <property name="prefix" value="/WEB-INF/views/" />      
    24.         <property name="suffix" value=".jsp" />      
    25.     </bean>      
    26.       
    27. </beans>   

    dispatcher0121-servlet.xml文件中也有要注意的地方,"example0121"这个是接下来会用的包,名字可以随意,但是不能全部是数字。我不想说,我就是偷懒用数字,导致之前建的项目一直有问题。害羞

    图10

    5、创建一个Controller层测试类(这个是什么东西,我也不知道。只知道怎么操作)

    图11

    按照这个操作,可以添加包也可以添加类,先添加类,要是发现在包那里没有写“example0121”,那就取消,然后添加一个包,包的名字是“example0121”。然后再创建一个叫“GeneralController”的类。

    图12

    然后将代码写好,像下面一样

    [html] view plain copy
     
    1. package example0121;    
    2.     
    3. import org.springframework.stereotype.Controller;      
    4. import org.springframework.ui.Model;      
    5. import org.springframework.web.bind.annotation.RequestMapping;      
    6.       
    7. @Controller      
    8. public class GeneralController {      
    9.       
    10.     @RequestMapping(value="index.do")      
    11.     public void index_jsp(Model model){      
    12.         model.addAttribute("str0121", "Hellow world");      
    13.         System.out.println("index.jsp");      
    14.     }      
    15. }      

    6、编写jsp页面。在“webapp/WEB-INF/”下添加一个文件夹“views”,再添加一个文件“index.jsp”。这个路径是在“dispatcher0121-servlet.xml”里面定下来的。

    [html] view plain copy
     
    1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>      
    2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>      
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">      
    4. <html>      
    5.     <head>      
    6.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">      
    7.         <title>Insert title here</title>      
    8.     </head>      
    9.           
    10.     <body>      
    11.         <c:out value="${str0121}"></c:out>      
    12.     </body>      
    13. </html>     

    这个时候你可以看到你的工程出现红色的叉叉,报了个错,如图13。这个时候你要淡定,两种方法。一是无视它,二是删掉这个错。我选择了无视它,我因为这个错查了好久资料,莫名烦躁,试了网上说的方法,还是不行。最后问了同事,同事说,你运行一下可不可以用。我运行了项目,发现可以用。然后同事说,那就无视它。似乎好像是校验的问题,反正现在不影响。one or two?!

    图13

    7、运行

    图14


    图15


    图16


    图17


    图18


    上面的博文地址为:http://blog.csdn.net/fulai0_0/article/details/42967481

    ——————-————————————分割线————————————————————————————————————————

    项目这样基本就算结束了,我在其中遇到的唯一问题是项目启动过程中无法启动。

    最后问了一下朋友,修改方法为在pom.xml文件中增加一句话:

     <packaging>war</packaging>

    位置如下:

     

    总体来说,对于Spring不熟悉的朋友搭建一个这种简单的mvc框架也是不容易的,自己搞了一天才弄明白,最后留几个比较有用的博文给大家参考

    http://www.cnblogs.com/davidwang456/p/4122842.html

    http://blog.csdn.NET/dddddz/article/details/8777126

    http://www.oschina.net/question/857812_220501

  • 相关阅读:
    访问网站出现EOF
    ExecutorService中submit()和execute()的区别
    JAVA设计模式之工厂模式
    使用cssQuery选择器语法来查找元素
    【转载 待读】卷积神经网络
    Python random模块(获取随机数)常用方法和使用例子
    Python 字符串操作方法大全
    Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat 解决办法
    如何在Windows 10安装和使用Linux的Bash shell
    python之内置函数
  • 原文地址:https://www.cnblogs.com/martin-roger/p/7218000.html
Copyright © 2011-2022 走看看