Spring3 REST MVC框架,提速你的Web开发

简介: 最近在Java web 项目中需要采用非常简单的REST框架,Struts2、webwork、JSF 经过一番比较,最后选择了Spring3,理由只有一个 “简单,好用,并满足需要”。很久以前就Rod Johnson大叔说 Spring3 全面支持REST风格的Web服务,"We'r...

最近在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,今天亲自尝试了一下,真有点相识恨晚的感觉,如果在这次项目运用没有太大的问题,将来在其他项目会大量运用。

工作原理如图所示:
http://1aqpcg.bay.livefilestore.com/y1p_Q90l9w2JCMiEtgzkdikNQZoyy7Ic7GiCrm8Uk2GUhHZ1C80d7j2Ty4X0IWuydFepV3htprYHjptpEtq561o-7Ok_rkBgoZa/spring_rest_fm.png
*根据HTTP请求的URL,调用相应的DispatcherServlet控制器。
*提供一个视图是作为HTTP响应发送。

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

http://1aqpcg.bay.livefilestore.com/y1pu1qm8gBjr1VJ_9hg2BvILVMcD3tnvyc1WTPRVpkVTHqbP00tQTml9vMDJU_xnyfNrGls2OPS7Df-Z96_zPSMFbqsWXxxUr2t/topic_view.png
主要代码:

清单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 www.JavaBloger.com ");
        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="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd 
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
            http://www.springframework.org/schema/jdbc 
            http://www.springframework.org/schema/jdbc/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" />

 

>


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

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

目录
相关文章
|
8天前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
40 7
|
3月前
|
开发框架 搜索推荐 数据可视化
Django框架适合开发哪种类型的Web应用程序?
Django 框架凭借其强大的功能、稳定性和可扩展性,几乎可以适应各种类型的 Web 应用程序开发需求。无论是简单的网站还是复杂的企业级系统,Django 都能提供可靠的支持,帮助开发者快速构建高质量的应用。同时,其活跃的社区和丰富的资源也为开发者在项目实施过程中提供了有力的保障。
159 62
|
2月前
|
监控 前端开发 API
一款基于 .NET MVC 框架开发、功能全面的MES系统
一款基于 .NET MVC 框架开发、功能全面的MES系统
|
2月前
|
Java 开发者 微服务
Spring Boot 入门:简化 Java Web 开发的强大工具
Spring Boot 是一个开源的 Java 基础框架,用于创建独立、生产级别的基于Spring框架的应用程序。它旨在简化Spring应用的初始搭建以及开发过程。
103 6
Spring Boot 入门:简化 Java Web 开发的强大工具
|
2月前
|
前端开发 安全 JavaScript
2025年,Web3开发学习路线全指南
本文提供了一条针对Dapp应用开发的学习路线,涵盖了Web3领域的重要技术栈,如区块链基础、以太坊技术、Solidity编程、智能合约开发及安全、web3.js和ethers.js库的使用、Truffle框架等。文章首先分析了国内区块链企业的技术需求,随后详细介绍了每个技术点的学习资源和方法,旨在帮助初学者系统地掌握Dapp开发所需的知识和技能。
2025年,Web3开发学习路线全指南
|
3月前
|
存储 前端开发 JavaScript
如何在项目中高效地进行 Web 组件化开发
高效地进行 Web 组件化开发需要从多个方面入手,通过明确目标、合理规划、规范开发、加强测试等一系列措施,实现组件的高效管理和利用,从而提高项目的整体开发效率和质量,为用户提供更好的体验。
55 7
|
3月前
|
开发框架 JavaScript 前端开发
TypeScript 是一种静态类型的编程语言,它扩展了 JavaScript,为 Web 开发带来了强大的类型系统、组件化开发支持、与主流框架的无缝集成、大型项目管理能力和提升开发体验等多方面优势
TypeScript 是一种静态类型的编程语言,它扩展了 JavaScript,为 Web 开发带来了强大的类型系统、组件化开发支持、与主流框架的无缝集成、大型项目管理能力和提升开发体验等多方面优势。通过明确的类型定义,TypeScript 能够在编码阶段发现潜在错误,提高代码质量;支持组件的清晰定义与复用,增强代码的可维护性;与 React、Vue 等框架结合,提供更佳的开发体验;适用于大型项目,优化代码结构和性能。随着 Web 技术的发展,TypeScript 的应用前景广阔,将继续引领 Web 开发的新趋势。
66 2
|
3月前
|
中间件 Go API
Go语言中几种流行的Web框架,如Beego、Gin和Echo,分析了它们的特点、性能及适用场景,并讨论了如何根据项目需求、性能要求、团队经验和社区支持等因素选择最合适的框架
本文概述了Go语言中几种流行的Web框架,如Beego、Gin和Echo,分析了它们的特点、性能及适用场景,并讨论了如何根据项目需求、性能要求、团队经验和社区支持等因素选择最合适的框架。
228 1
|
3月前
|
XML Java 网络架构
使用 Spring Boot 公开 SOAP Web 服务端点:详细指南
使用 Spring Boot 公开 SOAP Web 服务端点:详细指南
169 0
|
Java 测试技术 网络架构
Spring REST实践之客户端和测试
RestTemplate 可参考spring实战来写这部分。 RestTemplate免于编写乏味的样板代码,RestTemplate定义了33个与REST资源交互的方法,涵盖了HTTP动作的各种形式,其实这些方法只有11个独立的方法,而每一个方法都由3个重载的变种。
1273 0

热门文章

最新文章

  • 1
    打造高效的Web Scraper:Python与Selenium的完美结合
    13
  • 2
    Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
    26
  • 3
    AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
    20
  • 4
    【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
    54
  • 5
    部署使用 CHAT-NEXT-WEB 基于 Deepseek
    342
  • 6
    【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
    26
  • 7
    java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
    40
  • 8
    零基础构建开源项目OpenIM桌面应用和pc web- Electron篇
    28
  • 9
    【01】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-硬件设备实时监控系统运营版发布-本产品基于企业级开源项目Zabbix深度二开-分步骤实现预计10篇合集-自营版
    22
  • 10
    FastAPI与Selenium:打造高效的Web数据抓取服务 —— 采集Pixabay中的图片及相关信息
    55