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>


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