springMVC--注解

本文介绍如何使用SpringMVC进行注解开发,包括创建Web工程、配置web.xml及springmvc.xml、自定义Controller类等内容。同时讲解了请求映射@RequestMapping的各种用法及其参数配置。

注解

一、注解开发:

1、创建一个web工程,并导入jar包

 
jstl-1.2.jar
 

2、配置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" id="WebApp_ID" version="2.5">
  <display-name>springmvc2</display-name>
  
  <!-- 配置编码 -->
  <filter>
  	<filter-name>characterEncoding</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  </filter>
  
  <filter-mapping>
  	<filter-name>characterEncoding</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 加载springmvc.xml -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>  
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
</web-app>
 

3、配置springmvc配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
		
		<!-- 扫描,把Controller交给spring管理 -->
		<context:component-scan base-package="com.caixia"></context:component-scan>
		
		<!-- 配置注解处理器映射器 :寻找执行类Controller-->
	    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

		<!-- 配置注解处理器适配器:调用Controller方法,执行Controller -->
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
		
		<!-- 配置springmvc视图解析器 -->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/jsps/"></property>
			<property name="suffix" value=".jsp"></property>
		</bean>
				
</beans>


4、自定义Controller类

@Controller //<bean id="userController" class="UserController路径">
public class UserController {
	
	//请求映射注解
	@RequestMapping("hello")
	public String myHello(){
		
		//表示springmvc返回了一个逻辑视图hello
		return "hello";
	}
	

}
 

5、定义hello页面

根据视图解析,需要在web-inf下面定义jsps文件夹,在里面定义一个hello.jsp

 

  <body>
    欢迎访问springmvc
  </body>
 

 

注解开发流程:

 


RequestMapping

功能:请求映射

几种写法:

requestMapping(“hello”):可以匹配任何的扩展名

requestMapping(“/hello.do”)

requestMapping(value=”/hello.do”)

requestMapping(value=”/hello.do”,method=RequestMethod.GET)

requestMapping(value=”/hello.do”,method=RequestMethod.POST)

requestMapping(value=”/hello.do”, method={RequestMethod.GET, RequestMethod.POST})

 

浏览器直接访问,a标签都是get请求

表单提交(指定post),ajax指定post提交,post提交。

//请求映射注解
	@RequestMapping(value="/hello.do",method=RequestMethod.POST)
	public String myHello(){
		
		//表示springmvc返回了一个逻辑视图hello
		return "hello";
	}

如果是get请求 写成了post会报错
 

RequestMapping根路径

@RequestMapping("/user")
UserController{
	requestMapping("save")
	Save()
	requestMapping("update")
	Update{}
	requestMapping("find")
	Fiind()

}
项目名/user/save.do
@RequestMapping("/items")
ItemsController{

	requestMapping("save")
	Save()
	requestMapping("update")
	Update{}
	requestMapping("find")
	Find()
}
项目名/items/save.do
 

自定义根路径

 

requestParam:

value:参数名字,即入参的请求参数名字,如value=“studentid”表示请求的参数区中的名字为studentid的参数的值将传入;

required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报400错误码;

defaultValue:默认值,表示如果请求中没有同名参数时的默认值

定义如下:

public String userlist(@RequestParam(defaultValue="2",value="group",required=true) String groupid) {
}
 

形参名称为groupid,但是这里使用value="group"限定参数名为group,所以页面传递参数的名必须为group。

这里通过required=true限定groupid参数为必需传递,如果不传递则报400错误,由于使用了defaultvalue=”2”默认值即使不传group参数它的值为”2”,

所以页面不传递group也不会报错,如果去掉defaultvalue=”2”且定义required=true则如果页面不传递group则会报错。


 
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值