原文链接:http://blog.csdn.net/initphp/article/details/8208349

  • 1.创建一个web项目

  • 2.假设,我们已经安装完毕Spring所需要的依赖包,以及一些其它的扩展包,以及Jetty容器,ps:Jetty容器安装看上一节文章。


  • 3.运行web项目,必须有web.xml配置文件,web.xml放置在WebContent/WEB-INF/目录下面。

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    5. id="WebApp_ID" version="2.5">
    6. <!-- 配置文件位置,默认为/WEB-INF/applicationContext.xml -->
    7. <context-param>
    8. <param-name>contextConfigLocation</param-name>
    9. <param-value>/WEB-INF/applicationContext.xml</param-value>
    10. </context-param>
    11. <!-- 上下文Spring监听器 -->
    12. <listener>
    13. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    14. </listener>
    15. <!-- servlet控制跳转,这边默认回去走spring-servlet.xml的xml文件 -->
    16. <servlet>
    17. <servlet-name>spring</servlet-name>
    18. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    19. <init-param>
    20. <param-name>contextConfigLocation</param-name>
    21. <param-value>/WEB-INF/spring-servlet.xml</param-value>
    22. </init-param>
    23. </servlet>
    24. <!-- url-pattern 是Spring监听路由过来的方式,然后去寻找匹配的Controller
    25. 例如:
    26. <url-pattern>/</url-pattern>  一般是  /hello/say/ 这样的URL方式
    27. <url-pattern>*.htm</url-pattern> 一般是 /hello/say.htm 这样的URL方式
    28. -->
    29. <servlet-mapping>
    30. <servlet-name>spring</servlet-name>
    31. <url-pattern>/</url-pattern>
    32. </servlet-mapping>
    33. </web-app>
     
  • 4.运行Spring需要有applicationContext.xml这个配置文件,我们也将applicationContext.xml放置在WEB-INF/目录下。
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans
    3. xmlns="http://www.springframework.org/schema/beans"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:context="http://www.springframework.org/schema/context"
    6. xmlns:aop="http://www.springframework.org/schema/aop"
    7. xsi:schemaLocation="http://www.springframework.org/schema/beans
    8. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    9. http://www.springframework.org/schema/context
    10. http://www.springframework.org/schema/context/spring-context-2.5.xsd
    11. http://www.springframework.org/schema/aop
    12. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    13. ">
    14. <context:annotation-config/>
    15. <aop:aspectj-autoproxy/>
    16. </beans>
  • 5.spring-servlet.xml 配置了具体的Spring需要访问的Controller文件夹目录以及模板的目录和模板的后缀名称。
    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:context="http://www.springframework.org/schema/context"
    3. xmlns:p="http://www.springframework.org/schema/p"
    4. xmlns:mvc="http://www.springframework.org/schema/mvc"
    5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    8. http://www.springframework.org/schema/context
    9. http://www.springframework.org/schema/context/spring-context.xsd
    10. http://www.springframework.org/schema/mvc
    11. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    12. <!-- 访问com.mvc.rest包下有@Controller注解的Controller文件 -->
    13. <context:component-scan base-package="com.mvc.rest" />
    14. <!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
    15. <mvc:annotation-driven />
    16. <!-- 视图配置,配置试图的目录,后缀名等 -->
    17. <bean id="viewResolver"
    18. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    19. <property name="viewClass"
    20. value="org.springframework.web.servlet.view.JstlView" />
    21. <property name="prefix" value="/WEB-INF/views/" />
    22. <property name="suffix" value=".jsp"></property>
    23. </bean>
    24. </beans>
     
  • 6.我们需要创建一个com.mvc.rest包,在包下面创建一个名为:HelloController.java的文件。这个文件就是MVC的控制器。那么,我们有两个方法,分别为say和yes。url中访问分别是:/hello/say/和/hello/yes/
    1. package com.mvc.rest;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. //@Controller 是一个标识这个是控制器类的注解标签,如果是控制器类 都需要有这个注解。
    5. @Controller
    6. //@RequestMapping(value="/hello") 会映射到url /hello/则访问HelloController中的Action
    7. @RequestMapping(value="/hello")
    8. public class HelloController {
    9. //@RequestMapping(value="/say") 会映射到url /hello/say则访问HelloController中的Action
    10. @RequestMapping(value="/say")
    11. public void say() {
    12. System.out.print("this is HelloController And say Action \r\n");
    13. }
    14. @RequestMapping(value="/yes")
    15. public void yes() {}
    16. }
     
  • 7.我们需要在/WEB-INF/目录下创建一个views的目录,然后再创建一个/views/hello/的目录,里面分别是say.jsp和yes.jsp,是模板文件。
  • 8.然后运行配置,通过Jetty容器,运行web程序。
  • 9.运行完毕后,控制台会出现以下信息:
  • 10.最后在URL中分别访问:http://127.0.0.1:8090/hello/say/ 和 http://127.0.0.1:8090/hello/yes/ 成功!
  • 11.详细Spring Controller部分的注解,请阅读:http://zachary-guo.iteye.com/blog/1318597

最新文章

  1. 2016-1-28 图解HTTP(03)
  2. Activiti学习(一) 环境搭建
  3. (四)SQL Server分区管理
  4. underscore源码解析 (转载)
  5. PLSQL 的简单命令之二
  6. Struts2 validate校验
  7. Dede后台验证码不显示解决方法详解(dedecms 5.7)
  8. 【Android 界面效果16】关于android四大组件的总结
  9. 【转】Android项目中编译 C的模块
  10. Writing Your First Test
  11. hdu Intelligent IME
  12. SysLog解析
  13. VFL语言使用
  14. webpack性能优化——DLL
  15. Django 之 流程和命令行工具
  16. JSP 页面跳转的实现方法
  17. jenkins--svn+添加钩子去触发jenkins的job工作
  18. Swift5 语言参考(十) 语法汇总
  19. Android自己定义View之仪表盘
  20. struct in_addr 结构体

热门文章

  1. Jquery简介选择的
  2. WCF基金会
  3. Twitter实时搜索系统EarlyBird
  4. java 加载dll介绍(转)
  5. Cntlm安装和配置体验
  6. 从苹果系统InstallESD.dmg里提取IOS
  7. cocos2d-x box2d Demo注解
  8. Cocos2d-x 单点触摸--让我们用手指动起来的精灵
  9. 编hadoop-1.X源代码
  10. 设计模式--简单工厂VS工厂VS抽象工厂