
SpringMVC Hello World
发布日期:2021-05-16 03:11:02
浏览次数:21
分类:原创文章
本文共 3756 字,大约阅读时间需要 12 分钟。
前言
新年伊始,元宵佳节,窗外灯火通明,炮声连连。北漂以来第一次一个人在北京过十五。
切入正题,收假后一边要赶项目进度还要学习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搞定
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2025年05月06日 18时35分36秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux安装Redis 与 Redis基本语法
2023-02-04
Linux安装Redis及配置(超详细)
2023-02-04
Linux安装Tomcat
2023-02-04
Linux安装与配置SSH服务
2023-02-04
Linux安装卸载Mysql数据库
2023-02-04
linux安装卸载软件
2023-02-04
linux安装宋体
2023-02-04
LINUX安装源码软件经典三部曲
2023-02-04
linux安装目录
2023-02-04
Linux定时Job:crontab -e 与 /etc/crontab 的区别
2023-02-04
linux实用命令详解(新建删除复制文件夹,挂载) 转
2023-02-04
linux审计功能及规则 (audit.rule)
2023-02-04
Linux就这个范儿 第18章 这里也是鼓乐笙箫 Linux读写内存数据的三种方式
2023-02-04
linux屏蔽ip端口号,linux 防火墙打开端口/屏蔽IP等
2023-02-04
Linux工作目录切换命令
2023-02-04
Linux工作笔记022---查看Centos 内核版本号
2023-02-04
Linux工作笔记023---Centos7 查看系统安装了什么软件_多少软件
2023-02-04
Linux工作笔记024---Centos7 下查看本机公网IP
2023-02-04
Linux工作笔记025---CentOS7.3安装Nginx
2023-02-04