将之前的HelloWorld案例改为使用注解的形式,将更为简单。参考了学院的教程视频 http://edu.51cto.com/lesson/id-42166.html
1。新建Web Project,项目名称为springmvc002,复制之前项目springmvc001中的一些内容:
1) web.xml配置文件到WEB-INF目录下。
2)WebRoot/jsp下面的两个jsp页面文件 hello.jsp和showMsg.jsp
3) 通过Build Path 导入项目所需的spring mvc 的jar包
2。在WEB-INF目录下创建配置文件myspring-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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- springmvc 注解驱动 -->
<mvc:annotation-driven />
</pre><pre name="code" class="html"><!-- 扫描器 -->
<context:component-scan base-package="com.sunny"/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
和前一个项目相比,这里只保留了视图解析器的配置段,删除了原来的handlerMapping和Controller段。
并且在<beans> 头中添加了对mvc的引入。
添加了注解驱动和包自动扫描器。
3、创建Controller类
HelloController类:
</pre><pre name="code" class="java"><span style="font-size:14px;">package com.sunny.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(value="/hello.do")
public String hello(String userName,Model model){
model.addAttribute("uname",userName);
return "showMsg";//请求转到 WebRoot/jsp/showMsg.jsp
}
}
注意类名上方和方法名上方的注解,类名上的@Controller 表示这是一个注解,而方法名上方的@RequestMapping(value="/hello.do")则表示当请求的 url 是/hello.do时将执行此方法。
这里的方法中,参数userName是请求中的参数,因此一会儿要修改hello.jsp,将其中输入框的名称改为此名。
model中则是传给显示视图showMsg.jsp中的参数。
4、修改页面
将hello.jsp中输入框的名称改为userName,如下所示:
<form action="hello.do" method="post">
<!-- 下面的参数名称要与Controller中方法中的参数名称相同 -->
<input name="userName"/>
<input type="submit" value="send" />
</form>
将showMsg.jsp中要显示的参数名改为Controller中返回的uname,如下:
<body>
hello,${uname}
</body>
发布后访问http://localhost:8080/springmvc002/jsp/hello.jsp ,运行正常。
-----
后记:关于ReuestMapping的多个属性
1、其实,@RequestMapping这个注解也可以用在控制类前面,这时,访问的路径是类前的path+方法前的path,例如,下面的控制器类,访问的url是 http://localhost:8080/项目名称/springmvc/helloworld.do
@Controller
@RequestMapping ("/springmvc")
public class HelloWorldController {
@RequestMapping (value="/helloworld.do",method=RequestMethod.POST)
public String hello() {
return "hello";
}
}
在这里,方法hello前面的注解中的method表示对访问的method类型做了限制,这时不能以GET方式来访问。
这个method还可以是DELETE或PUT方式,但如果想支持这两种方式的请求,则必须修改web.xml,在其中添加HiddenHttpMethodFilter,然后可以在POST表单中以隐藏域的形式携带请求参数 _method,把这个参数的值设为 "DELETE"或"PUT",则可以原来的POST请求转变 DELETE或PUT请求。
2、如果在RequestMapping中加上params属性,则表示在发出请求时对参数的要求,如 Controller类中下面的方法:
@RequestMapping(value="/testParam",params= {"username","age!=0"})
public String testParam() {
System.out.println("testParam");
return "hello";
}
表示必须有参数username和age,而且age不能为0.
3、关于PathVariable
@RequestMapping("/testPathVar/{id}")
public String testPathVar(@PathVariable("id") Integer mid) {
System.out.println("id="+mid);
return "hello";
}
注解@PathVariable可以把url中的占位符id映射为方法的参数mid,从而可以在方法中进行处理。这个是SpringMVC向REST风格迈进的体现。