rest风格超简单案例



1使用maven构建一war工程

在pom.xml中配置tomcat插件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.qx</groupId>
    <artifactId>yirenbaomanagerparent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>yirenbaomanagerweb</artifactId>
  <packaging>war</packaging>
  
  <build>  
    <pluginManagement>  
        <plugins>  
            <!-- 配置Tomcat插件 -->  
            <plugin>  
                <groupId>org.apache.tomcat.maven</groupId>  
                <artifactId>tomcat7-maven-plugin</artifactId>  
                <version>2.2</version>  
                <configuration>  
                    <port>8085</port>  
                    <path>/</path>  
                </configuration>  
            </plugin>  
        </plugins>  
    </pluginManagement>  
  </build>  
</project> 


使用rest风格
何为rest风格?  路径化的参数.
为了能拦截rest风格的请求:
we.xml中这样配置Spring-MVC
映射路径: 写为 /  而不是  *.action或*.do
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>yirenbaomanagerweb</display-name>

	<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<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/spring-MVC.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>


接下来还需要一个spring-MVC配置文件和一个Controller,加一个目标页面


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: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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

	<context:component-scan base-package="com.qx"></context:component-scan>
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- <property name="prefix" value="/"></property> -->
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 配置resource标签,此标签内部的地址不会被拦截 -->
	<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
	<mvc:resources location="/script/" mapping="/script/**"></mvc:resources>
	<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>

	<mvc:annotation-driven></mvc:annotation-driven>
</beans>

PageController:

package com.qx.manager.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PageController {

	/**
	 * 返回值 字符串,如果没有加 responsebody注解,则代表返回值是一个页面的名字
	 * 如果以/开头,代表是自根路径开始找这个页面
	 * 如果不是/开头,它是自浏览器最后一个/开始替换掉后面的内容
	 * 如果是以redirect开头,代表是重定向,重定向这样写只能重定向到项目内部的页面
	 * 如果想要重定向到另外网站的页面,需要在地址前面添加http://  也就是目标页面的全路径
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping("/page/{page}") //把这个请求参数page取出来的值{page}当做参数传到getPage方法中赋给形参page
	public String getPage(@PathVariable("page") String page){
		return "/"+page;
	}
}


再在webapp下写一个页面hehe.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<table></table>

呵呵哒
<script type="text/javascript">

</script>

Run As>Maven build> tomcat7:run 启动项目内部tomcat,启动项目


使用rest风格,在地址栏中输入请求地址:  localhost:8085/page/hehe 时,  可以看到服务端给浏览器返回了一个页面就是咱们的hehe.jsp


虽然我在hehe.jsp页面中没有写<html>,<head>,<body>等标签,但当浏览器解析的时候你一定会发现这些标签一个都没少.全都有



  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值