原文链接: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

Spring搭建MVC WEB项目[转]的更多相关文章

  1. 搭建maven web项目并配置quartz定时任务【业务:对比数据变化内容】 历程

    搭建maven web项目并配置quartz定时任务[业务:对比数据变化内容] 历程2018年03月03日 10:51:10 守望dfdfdf 阅读数:100更多个人分类: 工作 问题编辑版权声明:本 ...

  2. 在Eclipse中使用Struts和Hibernate框架搭建Maven Web项目

    前言 学习使用Java还是2012年的事情,刚开始学习的Java的时候,使用的是MyEclipse工具和SSH框架.初学者适合使用MyEclipse,因为他将struts.Spring和Hiberna ...

  3. 【spring boot】5.spring boot 创建web项目并使用jsp作前台页面

    贼烦的是,使用spring boot 创建web项目,然后我再idea下创建的,but 仅仅启动spring boot的启动类,就算整个项目都是好着的,就算是能够进入controller中,也不能成功 ...

  4. angular2+typescript在asp.net MVC Web项目上的实现

    网上现在还没有关于angular2+typescript在asp.net mvc web项目上的实现的系统介绍,这里我也只是探索到了一个简单的方式,还有很多问题没能解决.但是能有个好的开头也值得记录一 ...

  5. maven3常用命令、java项目搭建、web项目搭建详细图解(转)

     转自:http://blog.csdn.net/edward0830ly/article/details/8748986 maven3常用命令.java项目搭建.web项目搭建详细图解 2013-0 ...

  6. Eclipse 搭建 Maven Web项目

    第一步:安装JDK: 第二步:安装Eclipse: 第三步:安装tomcat7: 第四步:安装maven插件: 4.1 下载maven:http://maven.apache.org/download ...

  7. 使用MyEclipse搭建java Web项目开发

    转自:http://blog.csdn.net/jiuqiyuliang/article/details/36875217 首先,在开始搭建MyEclipse的开发环境之前,还有三步工具的安装需要完成 ...

  8. 使用Spring Boot开发Web项目(二)之添加HTTPS支持

    上篇博客使用Spring Boot开发Web项目我们简单介绍了使用如何使用Spring Boot创建一个使用了Thymeleaf模板引擎的Web项目,当然这还远远不够.今天我们再来看看如何给我们的We ...

  9. 【java项目实战】一步步教你使用MyEclipse搭建java Web项目开发环境(一)

    首先.在開始搭建MyEclipse的开发环境之前.还有三步工具的安装须要完毕,仅仅要在安装配置成功之后才干够进入以下的java Web项目开发环境的搭建. 1.安装工具 第一步,下载并安装JDK,到官 ...

随机推荐

  1. 【v2.x OGE课程 14】 控制使用

    在这里,精灵.动画精灵.button天才.经常使用的文本的使用 一个.相关精灵 1.加入精灵 //创建精灵 Sprite bar_up = new Sprite(400, 0, RegionRes.g ...

  2. leetcode——Evaluate Reverse Polish Notation 求算式值(AC)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  3. json学习初体验--第三者jar包实现bean、List、map创json格式

    1.的需要jar包裹json-lib.jar 下载链接: http://sourceforge.net/projects/json-lib/files/json-lib/ 此包还须要下面的依赖包, c ...

  4. 取缔Chrome装载电脑管家的广告过滤脚本代码

    今天Chrome调试脚本.加载在下面的脚本中找到的内容: /* 电脑管家chrome 广告过滤 */ var GJAD_CS = { elemhideElt : null, setElemhideCS ...

  5. 使用android SpannableStringBuilder实现图文混排,看到许多其他

    项目开发需要达到这种效果 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmFuY3lsb3ZlamF2YQ==/font/5a6L5L2T/fontsiz ...

  6. datagrid直接编辑保存“设计缺陷”

    当今使用easyUI的datagrid组件的时候,碰到了一些问题,记录下来以便下次高速解决. 需求是在一张表单里会关联有一个列表,能够增删查改 曾经没用easyUI的时候,这个增和改的页面我通常是用一 ...

  7. hdu 1575 Tr A(矩阵高速电源输入)

    Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  8. VS2010使整个过程说明了安装包

    该项目的第一个版本出来,要成为一个包,很长一段时间没做了一些被遗忘,上差了差资料,写了一个,总结下,可能还不是非常完好,仅作參考. 1.首先在打开 VS2010    >新建>项目 2.创 ...

  9. Html5 拖放上传图片

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  10. T-SQL技术收集——删除重复数据

    原文:T-SQL技术收集--删除重复数据 在工作和面试中,经常出现如何查询或者删除重复数据的问题,如果有主键,那还好办一点,如果没有主键,那就有点麻烦. 当一个表上没有辅助键时,如果使用SSMS界面来 ...