spring rest初体验

之前项目一直使用spring+struts2的方式 ,看了下spring3的新特性(out了,4都出来了)--支持rest,于是试试,参考了Lee_cho举的例子,自己亲自测试了下,所需jar如下(这里全部下载的最新版本),目前spring官网改版了,下载jar包需要用mvn,找了半天都没找到入口


-----------------------------web.xml------------------------------

<?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <display-name></display-name>
      <context-param>
            <!--rest配置文件的路径,貌似不配置也是加载这个地址,这个地方有点疑问,大家指点指点-->
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/rest-servlet.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <servlet>
            <!-- 配置一个Servlet,有这个Servlet统一调度页面的请求 -->
        <servlet-name>rest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
            <!-- 映射路径,不要写成了/*那样会拦截所有的访问,连JSP页面都访问不了 -->
        <servlet-name>rest</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>  

--------------------------rest-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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    default-lazy-init="true">

    <description>Spring公共配置</description>

    <!--检测注解 -->
    <context:component-scan base-package="com.zhuzi.controller" />
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    <!-- 注册视图解析器,说白了就是根据返回值指定到某个页面 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/"></property> <!--页面文件的路径,在根目录下 -->
        <property name="suffix"><value>.jsp</value></property> 
    </bean>
</beans>  

--------------具体实现类--------SimpleController.java-----------------------
package com.zhuzi.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/simple")
public class SimpleController {
    // 映射路径/simple/index当访问这个路径时,执行这个方法
    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        // response,request会自动传进来
        request.setAttribute("message", "Hello,This is a example of Spring3 RESTful!");
        // 用来在同一个request周期中保存变量使用。比如servlet调用后,推出JSP页面,这是一个request周期,如果在Jsp页面需要servlet中的一些 处理结构,就从request.getAttribute中获取。
        return "index";
    }
    // 根据ID获取不同的内容,通过@PathVariable 获得属性
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String get(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setAttribute("message", "Hello,This is a example of Spring3 RESTful!<br/>ID:" + id + "");
        // response.getWriter().write("You put id is : "+id);
        return "index";
    }
}

---------------------index.jsp页面: ---------------------------

<%@ page language="java" pageEncoding="UTF-8"%>
<html>
  <head>
    <title>Spring3 RESTful</title>
   </head>
  
  <body>
    ${message}
   </body>
</html>

运行结果:
http://localhost:8080/springRest/simple/index  
Hello,This is a example of Spring3 RESTful!    
  
 http://localhost:8080/springRest/simple/2
Hello,This is a example of Spring3 RESTful!
ID:2  

比较容易出问题的就是下面的位置
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/"></property> <!--页面文件的路径,在根目录下 -->
        <property name="suffix"><value>.jsp</value></property> <!--后缀 -->

注意一定要能够找到你的jsp,否则就会出现下面的问题: 
警告: No mapping found for HTTP request with URI [/springRest/simple/index/] in DispatcherServlet with name 'rest'


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值