springMVC

第一个项目:HelloWorld


请求的顺序:

url /helloworld--》web.xml   springDispatcherServlet -->@RequestMapping注解来映射请求的URL /helloworld  返回success

由springmvc.xml   
/WEB-INF/views/success.jsp  转发到路径页面

1.创建动态web工程,

2.导入jar包



3.配置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_2_5.xsd" 
version="2.5">
<!-- 配置DispatcherServlet -->
<!-- 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>
		<!-- 配置DispatcherServlet初始化参数 :配置springMVC的配置文件的位置和名称-->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		
		</init-param>
		<!-- 在当前web应用在加载的时候被调用 而不是等第一次请求的时候被调用 -->
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern><!-- 可以应答所有请求 -->
	</servlet-mapping>

</web-app>
4.配置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"
	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.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<!-- 配置自动扫描的包 -->
<context:component-scan base-package="springMvc01.MVC"></context:component-scan>
<!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/views/"></property>
	<property name="suffix" value=".jsp"></property>
</bean>
</beans>

5.新建package springMvc01.MVC.handlers  在包下新建HelloWorld.java

package springMvc01.MVC.handlers;

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


@Controller
public class HelloWorld {
	
	/**
	 * 使用@RequestMapping注解来映射请求的URL
	 * 返回值会通过视图解析器为实际的物理视图,对于InternalResourceViewResolver视图解析器会做如下解析:
	 * 通过这样的方式:prefix+returnVal + 后缀   得到实际的物理视图,然后做转发操作
	 * /WEB-INF/views/success.jsp
	 * @return
	 */
	@RequestMapping("/helloworld")
		public  String hello() {
			System.out.println("hello world");
			return "success";
		}

}
6.新建index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="helloworld">helloworld</a>
</body>
</html>
7.新建在/WEB_INF/views包 success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Success
</body>
</html>
也可以这样配置:

<?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_2_5.xsd" 
version="2.5">
<!-- 配置DispatcherServlet -->
<!-- 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>
<!-- 配置DispatcherServlet初始化参数 :配置springMVC的配置文件的位置和名称-->
<!-- 实际上也可以不通过contextConfigLocation 来配置springmvc 文件 而使用默认的 -->
<!--<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>

</init-param>
默认的  文件:/WEB-INF/<servlet-name>-servlet.xml
也就是springmvc.xml 文件 放到/WEB-INF/ 下,文件名为:springDispatcherServlet-servlet.xml 
-->

<!-- 在当前web应用在加载的时候被调用 而不是等第一次请求的时候被调用 -->
<load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern><!-- 可以应答所有请求 -->
</servlet-mapping>


</web-app>


注解:@RequestMapping 除了可以修饰方法,还可以修饰类

package springMvc01.MVC.handlers;

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

@RequestMapping("/springmvc")
@Controller
public class SpringTest {

	private static final String SUSSESS="success";
	@RequestMapping("/testRequestMapping")
	public String testRequestMapping() {
		System.out.println("testRequestMapping");
		return SUSSESS;
	}
}

相应的index.jsp

<a href="springmvc/testRequestMapping">testRequestMapping</a>


@RequestMapping 的属性:

value:映射路径

method:GET/POST

params:参数
headers :请求头
@RequestMapping("/testAntPath/*/abc") 路径支持通配符的情况

package springMvc01.MVC.handlers;

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

@RequestMapping("/springmvc")
@Controller
public class SpringTest {

	private static final String SUSSESS="success";
	@RequestMapping("/testRequestMapping")
	public String testRequestMapping() {
		System.out.println("testRequestMapping");
		return SUSSESS;
	}
	/**
	 * 使用 method 属性指定请求方式(常用)
	 * @return
	 */
	@RequestMapping(value="testMethod",method=RequestMethod.POST)
	public String testMethod() {
		System.out.println("testMethod");
		return SUSSESS;
	}
	/**
	 * params 和 headers 来更加精确的映射请求,params 和 headers支持简单的表达式
	 * @return
	 *<a href="springmvc/testParamsAndHeaders?username=aa&age=9">testParamsAndHeaders</a>对应请求的url
	 */
	@RequestMapping(value="/testParamsAndHeaders",params = {"username","age!=10"},headers={"Accept-Language=zh-CN"})
	public String testParamsAndHeaders() {
		System.out.println("testParamsAndHeaders");
		return SUSSESS;
	}
	@RequestMapping("/testAntPath/*/abc")  //<a href="springmvc/testAntPath/mnscf/abc">testAntPath</a>对应请求的url
	public String testAntPath() {
		System.out.println("testAntPath");
		return SUSSESS;
	}
/**
	 * @PathVariable 可以来映射url中的占位符到目标方法的参数中
	 * @param id
	 *<a href="springmvc/testPathVariable/1">testPathVariable</a>对应请求的url
	 * @return
	 */
	@RequestMapping("testPathVariable/{id}")
	public String testPathVariable(@PathVariable("id") Integer id) {
		System.out.println("testAntPath"+id);
		return SUSSESS;
	}
}


Rest风格的URL:

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="springmvc/testMethod" method="POST">
<input type="submit" value="submit"></input>
 </form>
 <br><br> 
 <a href="springmvc/testRest/1">Test Rest get</a>
 <br><br>
<form action="springmvc/testRest/1" method="POST">
<input type="hidden" name="_method" value="DELETE"></input>
<input type="submit" value="Rest DELETE"></input>
 </form> 
 
 <form action="springmvc/testRest/1" method="POST">
<input type="hidden" name="_method" value="PUT"></input>
<input type="submit" value="Rest PUT"></input>
 </form> 
  <form action="springmvc/testRest" method="POST">
<input type="submit" value="POST"></input>
 </form> 
  <br><br>
<a href="helloworld">helloworld</a>
 <br>
<a href="springmvc/testPathVariable/1">testPathVariable</a>
<a href="springmvc/testAntPath/mnscf/abc">testAntPath</a>
<a href="springmvc/testRequestMapping">testRequestMapping</a>

<a href="springmvc/testParamsAndHeaders?username=aa&age=9">testParamsAndHeaders</a>
<a href="springmvc/testMethod">testMethod</a>
<a href="springmvc/testAntPath/mnscf/abc">testAntPath</a>
</body>
</html>
SpringTest.java

package springMvc01.MVC.handlers;

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;

@RequestMapping("/springmvc")
@Controller
public class SpringTest {

	private static final String SUSSESS="success";
	@RequestMapping("/testRequestMapping")
	public String testRequestMapping() {
		System.out.println("testRequestMapping");
		return SUSSESS;
	}
	/**
	 * 使用 method 属性指定请求方式(常用)
	 * @return
	 */
	@RequestMapping(value="testMethod",method=RequestMethod.POST)
	public String testMethod() {
		System.out.println("testMethod");
		return SUSSESS;
	}
	/**
	 * params 和 headers 来更加精确的映射请求,params 和 headers支持简单的表达式
	 * @return
	 */
	@RequestMapping(value="/testParamsAndHeaders",params = {"username","age!=10"},headers={"Accept-Language=zh-CN"})
	public String testParamsAndHeaders() {
		System.out.println("testParamsAndHeaders");
		return SUSSESS;
	}
	@RequestMapping("/testAntPath/*/abc")
	public String testAntPath() {
		System.out.println("testAntPath");
		return SUSSESS;
	}
	/**
	 * @PathVariable 可以来映射url中的占位符到目标方法的参数中
	 * @param id
	 * @return
	 */
	@RequestMapping("testPathVariable/{id}")
	public String testPathVariable(@PathVariable("id") Integer id) {
		System.out.println("testAntPath"+id);
		return SUSSESS;
	}
	
	/**
	 * Rest 风格的url
	 * 以CRUD为例
	 * 新增:/order POST
	 * 修改:/order/1 PUT   update?id=1
	 * 获取:/order/1 GET   get?id=1
	 * 删除:/order/1 DELETE  delete?id=1
	 * 
	 * 如何发送PUT请求和DELETE请求呢? 
	 * 1.需要配置HiddenHttpMethodFilter
	 * 2.需要发送POST请求
	 * 3.需要在发送POST请求时携带一个隐藏域name=“_method” 的隐藏域,值为DELETE或PUT
	 * 在springMVC中的目标方法中如何得到id呢
	 * 使用@PathVariable 注解
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/testRest/{id}" ,method=RequestMethod.GET)
	public String testRest(@PathVariable("id") Integer id) {
		System.out.println("testRestGET:"+id);
		return SUSSESS;
		
	}
	
	@RequestMapping(value="/testRest" ,method=RequestMethod.POST)
	public String testRest() {
		System.out.println("testRestPOST:");
		return SUSSESS;
		
	}
	
	@RequestMapping(value="/testRest/{id}" ,method=RequestMethod.DELETE)
	public String testRestDelete(@PathVariable("id") Integer id) {
		System.out.println("testRestDelete:"+id);
		return SUSSESS;
		
	}
	
	@RequestMapping(value="/testRest/{id}" ,method=RequestMethod.PUT)
	public String testRestPut(@PathVariable("id") Integer id) {
		System.out.println("testRestPut:"+id);
		return SUSSESS;
		
	}
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值