Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6541628
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: 系统运维

2011-06-08 22:31:08

最近在Java web 项目中需要采用非常简单的REST框架,Struts2、webwork、JSF 经过一番比较,最后选择了Spring3,理由只有一个 “简单好用,并满足需要”。很久以前就Rod Johnson大叔说 Spring3 全面支持REST风格的Web服务,"We're really seeing extensive interest and growth in REST, and it will have comprehensive support for RESTful Web services," said Johnson,今天亲自尝试了一下,真有点相识恨晚的感觉,如果在这次项目运用没有太大的问题,将来在其他项目会大量运用。

工作原理如图所示:
src=""
*根据HTTP请求的URL,调用相应的DispatcherServlet控制器。
*提供一个视图是作为HTTP响应发送。

页面上最终运行效果,如图所示:

src=""
主要代码:

清单1:TopicController

package com.javabloger.springrest.action;

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("/topic")  //url映射的名称
public class TopicController {

    @RequestMapping(value = "/{id}",method=RequestMethod.GET)
    public String helloWorld(
            @PathVariable Long id,
            HttpServletRequest request,
            HttpServletResponse response) {
            request.setAttribute("message", "You Input Topci Id is: "+id+"");
        return  "topic" ;   // 对应 /WEB-INF/jsp 目录下的 topic.jsp 文件
    }
       
    @RequestMapping(value="/add")
    public String test(HttpServletRequest request,  
            HttpServletResponse response){
        System.out.println("Hello ");
        request.setAttribute("message", "Hello JavaBloger ! ,@RequestMapping(value='/add')");
        return "topic";  // 对应 /WEB-INF/jsp 目录下的 topic.jsp 文件
       
    }
}

清单2 :UserController
package com.javabloger.springrest.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.javabloger.springrest.pojo.Users;

@Controller
@RequestMapping("/user")

public class UserController {
     @RequestMapping(value="/login")
    public String test(HttpServletRequest request,  
            HttpServletResponse response,Users  userinfo){   // 非常方便可以直接在方法里面放入对象
         if (userinfo.getUsername().equals("username") &&
                 userinfo.getPassword().equals("password")
             )
         {
             request.setAttribute("user", userinfo);
             return "users/list";   //判断,将跳转不同的页面
         }
         else{
             return "users/loginerr";  //判断,将跳转不同的页面
         }
    }
}

清单3:web.xml
     
      springmvc  
      org.springframework.web.servlet.DispatcherServlet  
      2  
  
  
   
     springmvc  
     /  
 
  

清单4:springmvc-servlet.xml

    xmlns:xsi="" xmlns:context=""
    xmlns:aop="" xmlns:p=""
    xmlns:tx="" xmlns:jdbc=""
    xsi:schemaLocation=" 
            /spring-beans-3.0.xsd 
             
            /spring-context-3.0.xsd 
             
            /spring-tx-3.0.xsd 
             
            /spring-jdbc-3.0.xsd">
            class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
   
   
            class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
   
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    value="org.springframework.web.servlet.view.JstlView" />
       
       
   

            class="org.springframework.context.support.ResourceBundleMessageSource"
        p:basename="i18n/messages" />

 

<<完整的Spring3 REST代码例子下载>>


一些废话:
我个人非常非常喜欢没有xml配置框架,因为项目一大不仅需要对代码进行维护对xml文件还需要维护相当繁琐,在这个项目中除了采用Spring3的REST特性,还采用了Apache的dbutils对数据进行操作,传说中的SSH轻量级框架是针对EJB和J2EE而言的轻量级,对于上述这样的框架SSH还能称得上“轻量级”吗?也许SSH目前成为了标准,但不再是“轻量级J2EE框架”的代名词!

另外,还有一份专门介绍 Spring3 MVC的资料《Spring 3 MVC CodeMash 2009》,请看幻灯片:

阅读(1310) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~