rest風格超簡單案例
阿新 • • 發佈:2019-02-03
1使用maven構建一war工程
在pom.xml中配置tomcat外掛
使用rest風格 何為rest風格? 路徑化的引數. 為了能攔截rest風格的請求: we.xml中這樣配置Spring-MVC 對映路徑: 寫為 / 而不是 *.action或*.do<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>
<?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>等標籤,但當瀏覽器解析的時候你一定會發現這些標籤一個都沒少.全都有