zoukankan      html  css  js  c++  java
  • springmvc复习笔记----Restful 风格,PathVariable获取 Url实例

    结构

    包与之前相同


    <?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>springmvc01</display-name>
      <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
        <servlet-name>springDispatcherServlet</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>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
      
      <filter>
        <filter-name>characterEncodingfilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>utf8</param-value>
        </init-param>
      </filter>
      <filter-mapping>
      <filter-name>characterEncodingfilter</filter-name>
      <url-pattern>*</url-pattern>
      </filter-mapping>
    </web-app>
    View Code
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
            xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!--   自动扫描加载注解的包 -->
    <context:component-scan base-package="com.ij34.bean"/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/view/"></property>
    <property name="suffix" value=".jsp" ></property>
    </bean>
    </beans>
    View Code

    web.xml 、spring-mvc.xml不变


    index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <% response.sendRedirect("articles/list")

    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">文章列表</th>
    </tr>
    <tr>
    <td>1</td>
    <td><a href="${pageContext.request.contextPath }/articles/show/1" target="_blank">文章一</a></td>
    </tr>
    <tr>
    <td>2</td>
    <td><a href="${pageContext.request.contextPath }/articles/show/2" target="_blank">文章二</a></td>
    </tr>
    </table>
    </body>
    </html>

    show.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>
    <p style="font-style: 16;font-weight:bold " >${articles.name}</p>
    <p>${articles.context }</p>
    </body>
    </html>

    Article.java

    package com.ij34.model;
    
    public class Article {
       private String id;
       private String name;
       private String context;
       public Article() {
        // TODO Auto-generated constructor stub   
    }
       public Article(String name,String context) {
        this.name=name;
        this.context=context;
        
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getcontext() {
        return context;
    }
    public void setcontext(String context) {
        this.context = context;
    }
       
    }

    Testhello.java

    package com.ij34.bean;
    
    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.ij34.model.Article;
    
    @Controller
    @RequestMapping("/articles")  //外部请求的 包括update.jsp里${students.name }
    public class Testhello {
    
      @RequestMapping("/list")   //@RequestMapping  请求映射
      public String list(Model model) {
           return "articles/list";
     }
    
      @RequestMapping("/show/{id}") 
      public ModelAndView show(@PathVariable("id")int id){//@RequestParam  请求参数   
           ModelAndView mav=new ModelAndView();
           if(id==1){
               mav.addObject("articles", new Article("文章一","这是文章1的内容!!!!!!!!"));
           }else if(id==2){
               mav.addObject("articles", new Article("文章二","这是文章2的内容!!!!!!!!"));
           }
           mav.setViewName("articles/show"); 
           return mav;
      }
    }

    结果

  • 相关阅读:
    nginx启动报错nginx: [error] open() "/usr/local/etc/nginx/logs/nginx.pid" failed
    JS实现斐波那契数列的几种方法
    CSS选择器有哪些?选择器的优先级如何排序?
    JS将扁平化的数据处理成Tree结构
    OpsAny-项目资源管理-cmdb表创建
    python异常的处理
    Linux系统安装java jdk
    mysql binlog日志解析
    MySQL 数据备份与同步
    linux下shell脚本中sed命令的用法
  • 原文地址:https://www.cnblogs.com/tk55/p/6655100.html
Copyright © 2011-2022 走看看