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

学习springmvc的注释搭建框架

2017-01-10 10:38 375 查看
今天来整合一下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页面就省略了,因为此不为重点
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MVC spring mvc 注释