前言

新年伊始,元宵佳节,窗外灯火通明,炮声连连。北漂以来第一次一个人在北京过十五。

切入正题,收假后一边要赶项目进度还要学习java,so在元宵佳节之际写了第一篇SpringMVC Hello World。

开发环境

MyEclipse 10、Tomcat 7、jdk1.7

项目结构及Spring依赖jar包

刚开始学Spring,还没用maven。

配置文件

开发Spring,配置文件自然少不了,web.xml、spring xml、spring mvc xml。

web.xml

配置spring mvc前端控制器DispatcherServlet及拦截request请求url-pattern

<!--监听spring 上下文 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root-context.xml</param-value>
</context-param> <!-- 配置Spring MVC DispatcherServlet -->
<servlet>
<servlet-name>MVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 加载spring mvc的xml到Spring上下文中 -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-context.xml</param-value>
</init-param>
</servlet>
<!--配置Spring DispatcherServlet拦截器 -->
<servlet-mapping>
<servlet-name>MVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

  

spring mvc xml

<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载Spring的全局配置文件 -->
<beans:import resource="root-context.xml" /> <!-- SpringMVC配置 -->
<context:component-scan base-package="com.autohome.controller" />
<mvc:annotation-driven />
<!-- 配置SpringMVC的视图渲染器, 让其前缀为:/ 后缀为.jsp 将视图渲染到/page/<method返回值>.jsp中 -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/views/" p:suffix=".jsp">
</beans:bean> </beans:beans>

  

root-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> </beans>

  

编写第一个Controller

采用注解的方式开发Controller主要使用属性Controller、RequestMapping

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/Home")
public class HomeController {
@RequestMapping("Index")
public ModelAndView index(){
ModelAndView view =new ModelAndView("index");
return view;
}
}

 方法中定义了ModelAndView对象,通过该对象指定所需要渲染的视图为index最后返回ModelAndView 将页面渲染到index.jsp中。 views视图文件夹我放在WEB-INF目录下,jsp没有什么内容,就不贴代码了,直接新建即可。

OK,部署tomcat,第一个Spring MVC搞定

最新文章

  1. 20145204&amp;20145212信息安全系统实验二
  2. Caffe fine-tuning 微调网络
  3. SEO站点优化学习总结
  4. Jsp与servlet的区别
  5. HDU 2896 病毒侵袭(AC自动机)
  6. Java--&gt;利用URL类下载图片
  7. js判断屏幕分辨率的代码
  8. script标签的位置
  9. OD调试6—使未注册版软件的功能得以实现
  10. Windows应用程序要点
  11. ReIn
  12. poj1830:开关问题
  13. [学习OpenCV攻略][012][读取、修改、保存图像]
  14. Linux 高性能服务器编程——Linux服务器程序规范
  15. WPF 10天修炼 第六天- 系统属性和常用控件
  16. IOC,DIP,DI,IoC容器
  17. org.activiti.engine.activitiexception:version of activiti database(5.22) is more recent than the engine(5.12)
  18. [转]微擎应用笔记3--manifest.xml文件使用说明
  19. Prometheus 监控进程
  20. CSU 1857 Crash and Go(relians)(模拟)

热门文章

  1. VBA找相似体积的单元格值
  2. xbeePRO900HP的几个关键参数
  3. PHP字符串截取函数
  4. # 2019-2020.3 《java程序设计》第一周学习总结
  5. 2019.03.02 bzoj2565: 最长双回文串(pam)
  6. shell脚本学习- 传递参数
  7. JavaScript基础视频教程总结(021-030章)
  8. 可遇不可求的Question之导入mysql中文乱码解决方法篇
  9. C语言中的语句
  10. 第三周Access的总结