博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springMVC demo搭建
阅读量:7113 次
发布时间:2019-06-28

本文共 2987 字,大约阅读时间需要 9 分钟。

1、使用idea新建一个基于maven的web项目,参考 http://www.cnblogs.com/winkey4986/p/5279820.html

2、采取了比较偷懒的配置方法,只配置了一个DispatcherServlet,注意区别同时配置DispatcherServletContextLoaderListener 

    (从长远考虑是需要同时配置的,DispatcherServelet中主要扫描Controller,参考 http://www.cnblogs.com/winkey4986/p/5280138.html)

3、先配置pom.xml

4.0.0
com.icebean
chicken
war
1.0-SNAPSHOT
chicken Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
org.springframework
spring-webmvc
4.2.4.RELEASE
org.springframework
spring-context
4.2.4.RELEASE
org.mortbay.jetty
maven-jetty-plugin
6.1.6
9999
60000
chicken

pom中配置了 spring-webmvc、spring-context,另外插件中配置了jetty web容器,指定端口为9999

4、“偷懒配法”web.xml

chicken
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
SpringMVC
/
index.jsp

对于web.xml中指定的spring配置文件applicationContext.xml(此文件放在resources中)配置

5、ok。配置文件好了,接下来是代码:

package com.icebean.demo;/** * Created by ***on 2016/3/15. */import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class HelloController {    @RequestMapping("/hello")    @ResponseBody    public String test() {        return "hello, world! This com from spring!";    }}

然后运行maven中的jetty插件,访问地址:http://localhost:9999/chicken/hello 搞定

总结一下:

1 创建一个标准的基于maven的web项目

2 两个maven依赖,spring-context;spring-mvc。maven就会自动下载所有关联的jar包,包括

  • spring-webmvc
  • spring-beans
  • spring-core
  • spring-expression
  • spring-web
  • spring-context
  • spring-aop
  • aopalliance
  • commons-logging

3 一个web.xml文件,配置了listener和servlet(可以只有一个DispatcherServelet)

4 两个spring相关的文件,applicationContext.xml和servletName-servlet.xml(可以只有一个applicationContext)
5 一个controller文件,配置了拦截的url处理代码

 

你可能感兴趣的文章
python ThreadPoolExecutor线程池使用
查看>>
IPTABLES 规则(Rules)
查看>>
关于URL编码
查看>>
深度学习的可解释性研究(一):让模型「说人话」
查看>>
QT5提示can not find -lGL的解决方法
查看>>
Silverlight/Windows8/WPF/WP7/HTML5周学习导读(9月17日-9月23日)
查看>>
Tap-Ahead:让移动搜索更加便捷的解决之道
查看>>
Windows Server2016 Hyper-v Cluster部署
查看>>
juniper路由器配置
查看>>
jQuery一点一滴系列教程(第三点)
查看>>
ARP解决方法/工具 真假ARP防范区别方法 ARP终极解决方案
查看>>
系统数据权限的实现方案
查看>>
华为vlan划分,单臂路由以及静态路由
查看>>
UCD 2010百度工作坊
查看>>
ssh2免密码登录
查看>>
4_move_find_into_model
查看>>
MySQL · 捉虫动态 · UK 包含 NULL 值备库延迟分析
查看>>
windows server 2012 standard Evaluation 安装试用
查看>>
windows server 2008中配置TCP/IP
查看>>
网管必读:交换机技术简介及应用分析
查看>>