您的位置:首页 > 编程语言 > Java开发

创建SpringMVC项目之使用注解实现

2016-08-26 15:22 513 查看
在之前我们使用配置文件实现SpringMVC的功能,还有另一种实现方式,通过注解来实现。

我们先来了解一下SpringMVC用到的注解名:

@Controller:负责注册一个bean 到spring 上下文中(和之前使用spring注解一样);

@RequestMapping:注解为控制器指定可以处理哪些 URL 请求;

@RequestParam:在处理方法参数处使用 @RequestParam 可以把请求参数传递给请求方法;

@ModelAttribute:在方法定义上使用 @ModelAttribute 注解:Spring MVC 在调用目标处理方法前,会先逐个调用在方法级上标注了@ModelAttribute 的方法;在方法的入参前使用 @ModelAttribute 注解:可以从隐含对象中获取隐含的模型数据中获取对象,再将请求参数–绑定到对象中,再传入入参将方法入参对象添加到模型中 

@SessionAttributes:用于声明session级别存储的属性,通常列出模型属性(如@ModelAttribute)对应的名称,则这些属性会保存到session中;

我们使用在上一篇文章创建SpringMVC项目之使用配置文件实现中创建好的项目,开始进行配置

一、新建配置文件beans.xml代替springmvc-servlet.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-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 
<!-- 要使注解生效,需要配置这几个属性,它会扫描demo.controller中的注解 -->
<context:component-scan base-package="demo.controller" />
<mvc:default-servlet-handler />
<mvc:annotation-driven />

<!-- 配置View层(视图层) -->
<!-- InternalResourceViewResolver类用于支持Servlet、JSP视图解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- viewClass:设置JstlView表示JSP页面可以使用JSTL标签库 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<!-- 设置过滤的请求的前缀 -->
<!-- <property name="prefix" value="/WEB-INF/jsp/" /> -->
<!-- 设置过滤的请求的后缀 -->
<property name="suffix" value=".jsp" />
<!-- 如传进来的逻辑视图名为hello,则该该jsp视图页面应该存放在“WEB-INF/jsp/hello.jsp” -->
</bean>

</beans>


二、修改web.xml中加载的配置文件为beans.xml,即classpath:beans.xml

三、新建类ProvinceAnnotationController,在类中使用注解

package demo.controller;

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

@Controller	//注册bean
@RequestMapping("/")	//通过此注解来为这个类映射一个URL,具体请求方法也配置路径则映射的路径为两者路径的叠加
public class ProvinceAnnotationController {

@RequestMapping("/hello.do")	//表示访问这个地址会启用login这个方法
public String login(){
return "hello";
}
@RequestMapping("/list.do")
public String list(){
return "list";
}
}


四、开启tomcat,访问地址http://localhost:8080/TestSpringMVC1/hello.do



这说明我们的注解基本配置成功了,然后怎么拿页面的信息和返回信息呢?

一种是在方法中加入和页面参数相同的属性名,如下

@RequestMapping("/hello.do")	//表示访问这个地址会启用login这个方法
public String login(String username){
System.out.println(username+",你好");
return "hello";
}
访问地址http://localhost:8080/TestSpringMVC1/hello.do?username=xxxxxx

服务端控制台显示



另一种是使用注解,可以让方法中的参数和页面参数取不一样的名称

@RequestMapping("/hello.do")	//表示访问这个地址会启用login这个方法
public String login(@RequestParam("username")String name){
System.out.println(name+",你好");
return "hello";
}


返回参数有多种方式,比如使用HttpServletRequest,ModelMap

@RequestMapping("/hello.do")	//表示访问这个地址会启用login这个方法
public String login(@RequestParam("username")String name,HttpServletRequest req){
System.out.println(name+",你好");
req.setAttribute("message", "hello world!你好!");
return "hello";
}
或HttpSession,

@RequestMapping("/hello.do")	//表示访问这个地址会启用login这个方法
public String login(@RequestParam("username")String name,HttpSession session){
System.out.println(name+",你好");
session.setAttribute("message", "hello world!你好!");
return "he
4000
llo";
}
或ModelMap

@Controller	//注册bean
@RequestMapping("/")	//通过此注解来为这个类映射一个URL,括号里是视图前缀,比如"/"换成"/pro",返回视图hello会变成访问/pro/hello.jsp
@SessionAttributes({"message"})//如果设置了这个注解,ModelMap相同值的生效范围是session
public class ProvinceAnnotationController {

@RequestMapping("/hello.do")	//表示访问这个地址会启用login这个方法
public String login(@RequestParam("username")String name,ModelMap map){
System.out.println(name+",你好");
map.put("message", "hello world!你好!");//默认放置到ModelMap里的值的生效范围是request
return "hello";
}......


三个测试的结果都是



注解中还有一个ModelAttribute,他有两个使用方式,一种是在方法头使用

@ModelAttribute//在调用其他方法前,调用这个方法
public void doBefore(){
System.out.println("------");
}


另一种是在参数前使用这个注解

@RequestMapping("/list.do") //如果@ModelAttribute注解添加到参数的前面,它会把ModelMap相应属性的值传入到参数中
public String list(@ModelAttribute("role")@RequestParam("username")String name){
System.out.println(name);
return "list";
}

@ModelAttribute//在调用其他方法前,调用这个方法
public void doBefore(ModelMap map){
System.out.println("------");
map.put("role", "管理员");
}
访问http://localhost:8080/TestSpringMVC1/list.do

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: