之前我们学会了如何用xml方式来实现springmvc,今天来看一下如何通过注解方式来实现springmvc
首先在一个web项目中导入需要的jar包:
在WEB-INF的web.xml中配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Servlet 前端控制器 springmvc核心控制器 -->
<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:spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 配置一个spring提供的针对post的中文乱码问题 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这样当我们访问*.action的时候,容器就会给我们找到classpath下的spring.xml该配置文件。
在classpath下建立spring.xml。
之前我们需要在spring.xml中配置,控制器,映射器,适配器,和视图解析器,在注解中,我们就可以在spring.xml中省略这些。只需要在spring.xml中配置扫描就可以。不要忘记引入context这些前提配置条件。
spring.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
">
<!-- 配置Action -->
<context:component-scan base-package="com.my.springmvc.ann"></context:component-scan>
</beans>
接下来我们来创建我们的控制类:
/**
* 单例模式
* 控制器
* @author user
*
*/
@Controller
public class HellloAction{
public HellloAction() {
// TODO Auto-generated constructor stub
System.out.println("helloAction = "+this.hashCode());
}
/*
* 业务方法
*/
//可以带参数可以不带参数,只要是/hello.action的请求,都交给hello()方法处理
@RequestMapping(value = "/hello.action")
public String hello(Model model) throws Exception{
System.out.println("helloActiono: 你好");
model.addAttribute("message", "第一个注解应用");
return "/success.jsp";
}
@RequestMapping(value = "/bye.action")
public String bye(Model model) throws Exception{
System.out.println("helloActiono: 再见");
model.addAttribute("message", "第一个注解应用");
return "/success.jsp";
}
}
下面以此解释一下各个注解的意思:
@Contorller:
表示该类是一个控制器类,相当于我们之前在xml中配置的bean标签。
@RequestMappsing(value = “hello.action”):
这个注解是加在方法上的,当我们访问hello.action的时候,请求的url会和RequestMappsing的value属性进行匹配,找到value和url对应的方法。
这样我们就可以在浏览器访问hello.action和bye.action