• SpringMVC rest风格的url


    无多余后缀,没有?!=那些乱七八糟的东西。

    但是rest风格也是有很多不好控制的东西,所以大部分的网站还是再用传统方式,稍好一点的会采用.html或.htm为后缀

    知识点:

    1.SpringMVC——对rest风格的支持

    2.@PathVariable获取url变量

    3.SpringMVC对静态资源的处理

    示例(模拟文章的点击查看)

    实体类:

    package com.maya.model;
    
    public class Article {
        private int id;
        private String title;
        private String content;
        public Article() {
            super();
            // TODO Auto-generated constructor stub
        }
        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;
        }
    }

    控制层

    package com.maya.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.maya.model.Article;
    
    @Controller
    @RequestMapping("article")
    public class ArticleController {
        @RequestMapping("/list")
        public String list(Model model){
            return "list";
        }
        @RequestMapping("/details/{id}")//请求的参数,可以跟多个所以用集合的方式
        public ModelAndView details(@PathVariable("id") int id){//@PathVariable获取url的变量
            ModelAndView mav=new ModelAndView();
            if(id==1){
                mav.addObject("article", new Article("文章一","文章一的内容"));
            }else if(id==2){
                mav.addObject("article", new Article("文章二","文章二的内容"));
            }
            mav.setViewName("details");//设置返回的视图名
            return mav;
        }
    }

    list.jsp

    <body>
    <table>
        <tr>
            <th colspan="2">
                <img alt="文章列表" src="${pageContext.request.contextPath}/resources/article_list.jpg">
            </th>                                                        <!-- 以resources就被被映射到images路径下 -->
        </tr>
        <tr>
            <td>1</td>
            <td>
                <a href="${pageContext.request.contextPath}/article/details/1" target="_blank">文章一</a>
            </td>                                    <!-- /article/details/1 : 带的参数id -->
        </tr>
        <tr>
            <td>2</td>
            <td>
                <a href="${pageContext.request.contextPath}/article/details/2" target="_blank">文章二</a>
            </td>
        </tr>
    </table>
    </body>

    details.jsp

    <link href="${pageContext.request.contextPath }/resources2/css.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
    <h1>${article.title }</h1>
    ${article.content }
    </body>

    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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 使用注解的包,包括子集 -->
        <context:component-scan base-package="com.maya"/>
        
        <!-- 注解驱动 -->
        <mvc:annotation-driven></mvc:annotation-driven>
        <!-- 这样所有以resources开头的都会被映射到images路径下 -->
        <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="/article/" /><!-- 返回视图到这个目录下 -->
            <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_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>SpringMVC3</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
      <!-- 同struts一样,也是需要拦截请求 -->
      <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><!-- 既然是rest风格的就不能只拦截后缀是.do的请求了,直接/,拦截所有请求才可以 -->
        </servlet-mapping>
    </web-app>
  • 相关阅读:
    好久不见(致win7)
    mysql update
    两个日期相差的天数
    java 文件指针复位
    shell编程: 获得目录下(包括子目录)所有文件名,路径和文件大小
    爬虫(2)
    爬虫
    eclipse 创建dynamic web project不能运行
    JSP Servlet之间交换数据
    html5视频播放插件
  • 原文地址:https://www.cnblogs.com/AnswerTheQuestion/p/6687121.html
Copyright © 2020-2023  润新知