`

Rest风格的资源URL

阅读更多
1. Restful风格的资源URL简介

2. SpringMvc对Rest风格的支持

3. @PathVariable获取Url变量

4. SpringMvc对静态资源的处理

新建项目SpringMvc03

Article.java

package com.andrew.model;
public class Article {
    private int id;
    private String title;
    private String content;
    public Article() {
        super();
    }
    public Article(String title, String content) {
        super();
        this.title = title;
        this.content = content;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}

ArticleController.java

package com.andrew.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.andrew.model.Article;
@Controller
@RequestMapping("/article")
public class ArticleController {
    @RequestMapping("/list")
    public String list(Model model){
        return "article/list";
    }
    @RequestMapping("/details/{id}")
    public ModelAndView details(@PathVariable("id") int id){
        ModelAndView mav=new ModelAndView();
        if(id==1){
            mav.addObject("article", new Article("文章一","文章一的内容"));
        }else if(id==2){
            mav.addObject("article", new Article("文章二","文章二的内容"));
        }
        mav.setViewName("article/details");
        return mav;
    }
}

spring-mvc.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:p="http://www.springframework.org/schema/p"
    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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 使用注解的包,包括子集 -->
    <context:component-scan base-package="com.andrew"/>
    <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="/images/"/>
    <mvc:resources mapping="/resources2/**" location="/css/"/>
    <!-- 视图解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>SpringMvc03</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <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:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

jsp/article/list.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<table>
    <tr>
        <th colspan="2">
            <img alt="文章列表" src="${pageContext.request.contextPath}/resources/article_list.jpg">
        </th>
    </tr>
    <tr>
        <td>1</td>
        <td>
            <a href="${pageContext.request.contextPath}/article/details/1" target="_blank">文章一</a>
        </td>
    </tr>
    <tr>
        <td>2</td>
        <td>
            <a href="${pageContext.request.contextPath}/article/details/2" target="_blank">文章二</a>
        </td>
    </tr>
</table>
</body>
</html>

jsp/article/details.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources2/css.css"/>
</head>
<body>
<p class="p1">${article.title }</p>
<p>${article.content }</p>
</body>
</html>

http://localhost:8080/SpringMvc03/article/list
运行结果:
1 文章一 
2 文章二
分享到:
评论

相关推荐

    第四章 Spring MVC Rest风格的url、静态资源标签

    在本章中,我们将深入探讨Spring MVC框架中的RESTful风格URL设计以及如何处理静态资源。REST(Representational State Transfer)是一种软件架构风格,常用于Web服务设计,它强调通过HTTP方法(如GET、POST、PUT、...

    rest风格webservice

    REST风格的Web服务通常使用HTTP协议,通过GET、POST、PUT、DELETE等HTTP方法来操作资源。 在RESTful服务中,每个URL(统一资源定位符)代表一个资源,通过HTTP方法来执行对资源的操作。例如,GET方法用于获取资源,...

    rest url详解

    RESTful URL是REST架构中的关键部分,用于表示资源和操作资源的方式。 在Spring框架中,Spring MVC提供了对RESTful风格的支持。使用Spring REST,我们可以轻松地创建处理HTTP请求的服务端点。接下来,我们将深入...

    springmvc之rest风格的crud

    在Spring MVC框架中,REST(Representational State Transfer)风格是一种设计Web服务的常见方式,它强调资源的概念,并通过HTTP协议中的方法(GET、POST、PUT、DELETE)来操作这些资源。本教程主要针对初学者,讲解...

    REST构架风格.doc

    * 资源为核心:REST 架构风格的核心概念是资源,它让我们从资源的角度来审视互联网应用。 * 状态表述转移:在 REST 的世界中,资源即状态,而互联网就是一个巨大的状态机。 * 无状态服务器:REST 风格应用可以实现...

    Rest风格项目中遇到的问题

    在开发基于REST(Representational State Transfer)风格的项目时,我们常常会遇到一系列问题,这些问题可能涉及API设计、状态管理、错误处理、安全性和性能优化等多个方面。REST是一种架构风格,用于构建分布式系统...

    WEB框架——REST原理(架构风格与基于网络的软件架构设计)

    REST风格的架构强调简洁、高效和可扩展性,它基于HTTP协议,利用其固有的方法(GET、POST、PUT、DELETE等)来操作资源。 在RESTful架构中,**资源(Resource)**是核心概念,每个资源都有一个唯一的URI(Uniform ...

    REST URI(URL是其子类)命名规范.zip_子类构造方法

    REST(Representational State Transfer)是一种网络应用程序的设计风格和开发方式,基于HTTP协议,以简洁和标准化的方式进行数据交换。URL(统一资源定位符)是URI的一个子类,主要用于定位网络资源。 1. **URI中...

    用于创建 REST风格系统的框架

    ### 用于创建 REST 风格系统的框架 #### 表述性状态转移 (REST) 概念 表述性状态转移(Representational State Transfer,简称 REST)是一种架构风格,旨在简化客户端和服务端之间的交互,尤其适用于分布式环境中...

    springMVC rest风格视图解析

    本篇文章将深入探讨Spring MVC如何实现REST风格的视图解析,以及如何根据请求的后缀名返回JSON或XML格式的数据。 首先,我们需要理解Spring MVC中的DispatcherServlet是如何工作的。DispatcherServlet是Spring MVC...

    Rest风格前后端分离 web项目demo

    **前后端分离与REST风格** 在现代Web应用开发中,前后端分离已经成为了一种主流模式。这种模式将前端用户界面的开发与后端业务逻辑的处理分离开来,以提高开发效率,提升用户体验,同时也更好地适应了敏捷开发的...

    使用rails编写REST风格的web应用.pdf

    标题与描述均指向了"使用Rails编写REST风格的Web应用"这一主题,这是一份深入探讨如何运用Ruby on Rails框架来构建遵循REST(Representational State Transfer)架构风格的Web应用程序的指南。REST作为一种架构风格...

    [示例][PHP]drest-master将Doctrine实体展现为REST资源结点的库.zip

    REST从资源的角度类审视整个网络,它将分布在网络中某个节点的资源通过URL进行标识,客户端应用通过URL来获取资源的表征,获得这些表征致使这些应用转变状态 所有的数据,不过是通过网络获取的还是操作(增删改查)...

    Spring+dubbox+rest风格整合demo

    Spring整合dubbox框架编写rest风格,项目使用maven构建,需要编译dubbo-2.8.4到本地的maven资源仓库,然后使用当前代码测试,不需要相应的web容器,将浏览器作为一个客户端访问URL。

    SpringMVC精品资源--RestSpringMVCDemo项目是一个基于Spring的符合REST风格的项目,.zip

    REST风格的核心原则是通过HTTP方法(GET、POST、PUT、DELETE等)来操作资源。在RestSpringMVCDemo项目中,我们可能会看到Controller类中定义了这些HTTP方法,每个方法处理特定的URL请求,代表对某个资源的操作。例如...

    应用Rails进行REST开发

    1. **简明的URL**:REST风格的URL旨在定位资源而非调用某个操作。通常情况下,URL会包含控制器名称和资源ID,而具体的操作则隐含在HTTP方法中(例如GET、POST、PUT、DELETE)。 2. **多格式内容响应**:通过一种...

    Struts2 Rest方式和非Rest方式共存

    在Struts2中,可以通过配置特定的ActionMapper来支持REST风格的URL映射。`RestActionMapper`是Struts2提供的一个关键组件,它负责将RESTful请求映射到相应的Action上。 首先,让我们了解非REST方式的Struts2工作...

    REST风格前后端分离框架(mavne分模块)

    REST风格的前后端分离框架是现代Web开发中的一个重要概念,旨在提高系统的可伸缩性、灵活性和维护性。在这个框架中,"REST"代表表述性状态转移(Representational State Transfer),它是一种网络应用程序的设计风格...

Global site tag (gtag.js) - Google Analytics