24、(知識篇)SpringMVC01(SpringMVC HelloWorld)

/**

* SpringMVC HelloWorld

* 1、在web.xml中配置dispatcherservlet(如果eclipse中配置了spring插件可以用alt+/進行快速配置)

* 2、在spring-mvc.xml中配置InternalResourceViewResolver的bean

* 配置前序和後續

* ============================================================

* 3、使用requestmapping配置路徑

* 格式爲 prefix+requestmapping返回的路徑+suffix

* 其中requestmapping可以修飾在類上面

* 4、使用method指定請求方式:get、post、put、delete

* 5、指定params 則必須帶有指定參數,並且必須滿足參數條件,才能訪問函數,不滿足則報404,可以指定簡單表達式

* 6、指定headers,與指定params一樣,必須滿足表達式才能訪問方法,不滿足則報404

* ============================================================

* 7、配置ant方式訪問 參考sayhello2方法

requestmapping中配置

* /*/方法名 則訪問時,可以用任意字符 訪問例如:http://127.0.0.1:8080/24SpringMVC01/Test/asdfasdf/sayHello2

* /**/方法名  如果兩個*號,代表任意路徑

* 8、@PathVariable 參考sayHello3方法

* /sayHello3/{xxx} 然後使用註解 @PathVariable(value="xxx")  類型 參數名

  例如:http://127.0.0.1:8080/24SpringMVC01/Test/sayHello3/1/2

* @return

*/


測試類:

package com.spring.test;

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

@Controller
@RequestMapping("Test")
public class Test {
	
	
	/**
	 * SpringMVC HelloWorld
	 * 
	 * 1、在web.xml中配置dispatcherservlet(如果eclipse中配置了spring插件可以用alt+/進行快速配置)
	 * 2、在spring-mvc.xml中配置InternalResourceViewResolver的bean
	 * 		配置前序和後續
	 * ============================================================
	 * 3、使用requestmapping配置路徑
	 * 	格式爲 prefix+requestmapping返回的路徑+suffix
	 * 	其中requestmapping可以修飾在類上面
	 * 
	 * 4、使用method指定請求方式:get、post、put、delete
	 * 5、指定params 則必須帶有指定參數,並且必須滿足參數條件,才能訪問函數,不滿足則報404,可以指定簡單表達式
	 * 6、指定headers,與指定params一樣,必須滿足表達式才能訪問方法,不滿足則報404
	 * ============================================================
	 * 
	 * 7、配置ant方式訪問 參考sayhello2方法
	 * 	 	在requestmapping中配置
	 * 		/*/方法名 則訪問時,可以用任意字符 訪問例如:http://127.0.0.1:8080/24SpringMVC01/Test/asdfasdf/sayHello2
	 * 		/**/方法名  如果兩個*號,代表任意路徑
	 * 
	 * 8、@PathVariable 參考sayHello3方法
	 * 		/sayHello3/{xxx} 然後使用註解 @PathVariable(value="xxx")  類型 參數名
	 * 	   例如:http://127.0.0.1:8080/24SpringMVC01/Test/sayHello3/1/2
	 * 
	 * @return
	 */
	
	@RequestMapping(value="sayHello",method=RequestMethod.GET,params={"name","age","sex!=0"},headers={"Cache-Control=max-age=0"})
	public String sayHello(){
		System.out.println("Hello...");
		return "index";
	}
	
	@RequestMapping("/*/sayHello2")
	public String sayHello2(){
		System.out.println("sayHello2...");
		return "index";
	}
	
	@RequestMapping("/sayHello3/{page}/{currentpage}")
	public String sayHello3(@PathVariable(value="page") int page,@PathVariable(value="currentpage") int currentpage){
		System.out.println("page == "+page+" currentpage == "+currentpage);
		return "index";
	}
	
}

springmvc.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"
	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">
	
	<context:component-scan base-package="com.spring.test"></context:component-scan>
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

web.xml

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>24SpringMVC01</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-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>

web-inf/views/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	Hello Spring!
</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章