1. RESTful 风格
REST(英文:Representational State Transfer,简称 REST),即表述性状态传递。RESTful 是一种设计风格,而不是标准,只是提供了一组设计原则和约束条件。
简单的讲,RESTful 风格就是将 URL 的请求参数变为请求路径,目的就是让一个URL看起来更加简洁实用、安全。举一个例子 。
原 url :http://localhost:8080/helloUser.form?userName=lucky&age=100
RRESTful 风格的 url:http://localhost:8080/helloUser/lucky/100
2. 工程目录
工程目录如下图所示,可参考创建springMVC的入门工程(SpringMVC入门实例,点击前往)。
3. 源文件编写
3.1 java 文件编写
HelloUserController.java,控制器类,本工程最重要的一部分。
url 为 /helloUser/{userName}/{age},使用 @PathVariable("userName") 、@PathVariable("age")分别获得 {userName},{age}。如 http://localhost:8080/helloUser/lucky/100 , {userName} = lucky ,{age} = 100。
源文件如下:
package com.study.springmvc.controller;
import com.study.springmvc.model.User;
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.bind.annotation.RequestMethod;
// @Controller 标识是一个控制器
@Controller
public class HelloUserController {
// 访问 http://localhost:8080/helloUser/lucky/100 , userName = lucky , userName=100
@RequestMapping(value = "/helloUser/{userName}/{age}", method = {RequestMethod.GET})
public String handleRequest1(Model model, @PathVariable("userName") String userName, @PathVariable("age") int age) {
User user = new User();
user.setUserName(userName);
user.setUserAge(age);
// 将接受到的数据传过去
model.addAttribute("user", user);
return "helloUser";
}
}
除了get这种请求方式:method = {RequestMethod.GET}, 还有另外几种 ,如 post, put, delete ,分别对应 method = {RequestMethod.POST} 、method = {RequestMethod.PUT}、method = {RequestMethod.DELETE},以下是对 几种 请求方式的介绍,选自 百度百科。
- GET :获取由请求URL标识的资源。调用服务器的资源,并将其作为响应返回给客户端.doGet()调用在URL里显示正在传送给Servlet的数据,这在系统的安全方面可能带来一些问题,比如说,用户登录时,表单里的用户名和密码需要发送到服务器端,doGet()调用会在浏览器的URL里显示用户名和密码.
- .POST :向Web服务器发送无限制长度的数据。它用于把客户端的数据传给服务端,使用它可以以隐藏方式给服务器端发送数据.Post适合发送大量数据.
- PUT :存储一个资源到请求的URL。调用和doPost()相似,并且它允许客户端把真正的文件存放在服务器上,而不仅仅是传送数据.
- DELETE :删除由URL标识的资源。 它允许客户端删除服务器端的文件或者Web页面.它的使用非常少.
User.java,源码如下:
package com.study.springmvc.model;
public class User {
private String userName;
private int userAge;
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", userAge=" + userAge +
'}';
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
}
test.java,源码如下:
package com.study.springmvc;
public class test {
}
3.2 xml 文件编写
web.xml,源码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- SpringMVC 的前端控制器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 拦截所有URL请求,Web 应用接收到该类请求之后 , 会调用前端控制器-->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-servlet.xml,源码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 扫描配置 -->
<context:component-scan base-package="com.study.springmvc.controller"/>
<mvc:annotation-driven />
</beans>
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- empty -->
</beans>
3.3 视图源代码
helloUser.jsp, 展示界面,源码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户信息展示</title>
</head>
<body>
<h1>用户姓名:${user.userName}</h1>
<h1>用户年龄:${user.userAge}</h1>
${message}
</body>
</html>
index.jsp,源码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎界面</title>
</head>
<body>
<%-- empty --%>
</body>
</html>
4. 工程测试
访问 http://localhost:8080/helloUser/lucky/100 ,结果如图所示。
END。