结构

包与之前相同


<?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>
<?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>

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;
}
}

结果

springmvc复习笔记----Restful 风格,PathVariable获取 Url实例的更多相关文章

  1. springMVC+json构建restful风格的服务

    首先.要知道什么是rest服务,什么是rest服务呢? REST(英文:Representational State Transfer,简称REST)描写叙述了一个架构样式的网络系统.比方 web 应 ...

  2. SpringMVC学习笔记-REST风格请求实现

    RESTful概念及功能 RESTful的概念:RESTful是 一种资源定位及资源操作的风格,其本身既不是标准也不是协议,而是一种设计风格,可以使得软件整体层次更加分明.代码更加简洁,并且有利于实现 ...

  3. springMVC中添加restful 风格

    RESTful架构:是一种设计的风格,并不是标准,只是提供了一组设计原则和约束条件,也是目前比较流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. 关于 ...

  4. springMvc框架之Restful风格

    method: @Controller @RequestMapping("/test") public String MyController{ @RequestMapping(& ...

  5. springmvc+swagger构建Restful风格文档

    本次和大家分享的是java方面的springmvc来构建的webapi接口+swagger文档:上篇文章分享.net的webapi用swagger来构建文档,因为有朋友问了为啥.net有docpage ...

  6. 《一头扎进SpringMvc视频教程》Rest风格的资源URL

    5.@PathVariable和@RequestParam的区别 请求路径上有个id的变量值,可以通过@PathVariable来获取  @RequestMapping(value = "/ ...

  7. springmvc中配置RESTful风格控制器

    一般的http请求中其实只需要get和post就可以满足项目需求了,而为什么还要使用restful可能就是为了使请求url看起来更加直观,好看吧.. restful常用的请求方式:get,post,p ...

  8. springmvc复习笔记----文件上传multipartResolver

    结构                                              web.xml <?xml version="1.0" encoding=&q ...

  9. springmvc复习笔记----springmvc姓名年龄例子:RequestParam 试水

    继续 继上节http://www.cnblogs.com/tk55/p/6652394.html 重要部分颜色突出 结构 包 web.xml 乱码处理方面设置 <url-pattern>* ...

随机推荐

  1. Chapter 4 Invitations——5

    And that was the last contact I'd had with him, though he was there, a foot away from me, every day. ...

  2. Coder解压探索===冥想补蓝v.1.0

    主题是什么? 这是一篇是我自己在探索冥想术的过程中,有了一些浅薄的收获,所以写出来记录与分享. 我不太记得最早是因为什么原因去学习冥想,一开始对我而言,这个词带有很多成见,诸如“老僧入定”“三界六道” ...

  3. Python面向对象基础:设置对象属性

    用类存储数据 类实际上就是一个数据结构,对于python而言,它是一个类似于字典的结构.当根据类创建了对象之后,这个对象就有了一个数据结构,包含一些赋值了的属性.在这一点上,它和其它语言的struct ...

  4. go基础系列:结构struct

    Go语言不是一门面向对象的语言,没有对象和继承,也没有面向对象的多态.重写相关特性. Go所拥有的是数据结构,它可以关联方法.Go也支持简单但高效的组合(Composition),请搜索面向对象和组合 ...

  5. 关于 Cortex-M3 的双堆栈机制

    CM3 的堆栈分为两个:主堆栈和进程堆栈. 那么,这两个栈分别在什么情况下使用呢? 我们看一下CM3的控制寄存器(CONTROL):控制寄存器用于定义特权级别,还用于选择当前使用哪个堆栈指针. CON ...

  6. Jenkins持续集成介绍及插件安装版本更新演示(一)--技术流ken

    Jenkins介绍 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. Jenkins功能包括: ...

  7. JDK源码分析(一)—— String

    dir 参考文档 JDK源码分析(1)之 String 相关

  8. iOS main.m解析

    在iOS开发中,有一个文件main.m,可能并不是很引起开发的注意.不过,可能在面试过程中,面试官还是有些会问到主函数里面到底做了哪些工作和任务.下面我们主要看一下main.m内部的逻辑. #impo ...

  9. [转]angular2: including thirdparty js scripts in component

    本文转自:https://stackoverflow.com/questions/35570746/angular2-including-thirdparty-js-scripts-in-compon ...

  10. 腾讯云图片鉴黄集成到C#

    官方文档:https://cloud.tencent.com/document/product/641/12422 请求官方API及签名的生成代码如下: var urlList = new List& ...