学习springmvc的注释搭建框架

来源:互联网 发布:自学网络编程 编辑:程序博客网 时间:2024/06/03 06:06

今天来整合一下springMVC注释搭建,在使用了注释之后,开发效率明显提高了,与strut2相差无几,甚至在运行效率上还要比strut快一点。


第一步。导入相关的包至lib文件夹。

第二步。在web.xml中配置dispatcherServlet。

  <context-param>  
        <param-name>contextConfigLocation</param-name>  
  
        <param-value>/WEB-INF/springmvc-servlet.xml</param-value>  
    </context-param>  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    
    <servlet>  
        <servlet-name>springmvc</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    
    <servlet-mapping>  
        <servlet-name>springmvc</servlet-name>  
        <url-pattern>*.do</url-pattern>  
    </servlet-mapping>  


第三步。添加xxx-servlet.xml文件

首先添加此bean。

<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
  xmlns:p="http://www.springframework.org/schema/p"  
  xmlns:mvc="http://www.springframework.org/schema/mvc"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 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.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

启动注解驱动的Spring MVC功能。

<mvc:annotation-driven />  

启动包扫描功能

<context:component-scan base-package="com.springmvc.controller" />  

配置视图解析器

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:suffix=".jsp" />  

第四步。创建controller。


@Controller
public class HelloWorld {
@RequestMapping(value="/welcome")
public String Welcome(){
System.out.println("welcome bro!!!");
return "welcome";
}
}
其中@controller标志这个类可以作为请求处理类 ,也就是 控制类。@RequestMapping用来处理对应的请求,其中的value为请求的名字,header可以设置请求头,method可以设置提交的方式,post或者get。

jsp页面就省略了,因为此不为重点

1 0