java之spring mvc之helloworld
这篇主要讲解springmvc的基本的使用,这里以helloworld项目为例。
目录结构:

1. 新建 web 项目 :springmvc_helloworld

2. 在 WebRoot\WEB-INF\lib 下,导入jar包
commons-logging-1.1.1.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
3. 在 WebContent\WEB-INF下,编写web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>01springmvc_helloworld</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
上面代码中标识的 springmvc 为前端控制器
4. 在 src 下编写 HelloController类
package cn.vincent.controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
* 实现Controller接口,默认处理方法是handleRequest
*
*/
public class HelloController implements Controller{ @Override
public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse resp) throws Exception {
// TODO Auto-generated method stub
System.out.println("hello world 被执行");
ModelAndView mv=new ModelAndView();
mv.addObject("msg", "第一个springmvc应用程序");
mv.setViewName("hello"); return mv;
} }
5. 编写 spring mvc 配置文件,该配置文件默认放于 WEB-INF 下,并且文件名为 前端控制器名-servlet.xml
这里的名称为 springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
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"> <!-- 配置handlerMapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 配置handlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 配置Controller -->
<bean name="/hello.do" class="cn.vincent.controller.HelloController"/>
<!-- 配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 为响应的视图名称加上前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 为响应的视图名称加上后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
6. 编写 jsp
在 WebRoot/WEB-INF/jsp 文件夹下 添加 hello.jsp 文件
hello.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>springmvc第一个程序</title>
</head>
<body>
${msg }
</body>
</html>
7.测试
这里可以直接这样输入:
http://localhost:8080/springmvc_helloworld/hello.do
也可以修改这样:
(1)在项目上右键,点击属性(Properties)

(2)修改 context-root

之后,重新部署,运行项目,输入地址
http://localhost:8080/helloworld/hello.do
8.效果如下

github地址:https://github.com/Vincent-yuan/springmvc_helloworld
java之spring mvc之helloworld的更多相关文章
- 框架-Java:Spring MVC
ylbtech-框架-Java:Spring MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 We ...
- 从零开始学 Java - 搭建 Spring MVC 框架
没有什么比一个时代的没落更令人伤感的了 整个社会和人都在追求创新.进步.成长,没有人愿意停步不前,一个个老事物慢慢从我们生活中消失掉真的令人那么伤感么?或者说被取代?我想有些是的,但有些东西其实并不是 ...
- Java框架-Spring MVC理解001
Spring MVC理解 1.servlet--Spring MVC的本质 2.Spring MVC其实是一个工具,具体的理解可以分为两步:第一步,了解这个工具是怎么创建出来的:第二步,了解这个工具是 ...
- Java之Spring mvc详解
文章大纲 一.Spring mvc介绍二.Spring mvc代码实战三.项目源码下载四.参考文章 一.Spring mvc介绍 1. 什么是springmvc springmvc是sprin ...
- 【Java】Spring MVC 扩展和SSM框架整合
开发web项目通常很多地方需要使用ajax请求来完成相应的功能,比如表单交互或者是复杂的UI设计中数据的传递等等.对于返回结果,我们一般使用JSON对象来表示,那么Spring MVC中如何处理JSO ...
- java之spring mvc之初始spring mvc
1. mvc : mvc框架是处理 http请求和响应的框架 2. mvc 做的事情有哪些: 将 url 映射到一个java的处理方法上 将表单数据提交到 java 类中 将后台 java 类处理的结 ...
- 利用maven构建一个spring mvc的helloworld实例
刚开始学习maven和spring mvc,学的云里雾里的 这里提供一个hello world实例,记录自己的学习之路 首先看maven官网的介绍 Apache Maven is a software ...
- 从零开始学 Java - 搭建 Spring MVC 记录云创的日子 第一章
2017年11月29日 来到新项目,需要用到Spring MVC ,那么我就开始记录我这次的学习. Spring MVC 框架是围绕一个 DispatcherServlet 来设计的,这个 Servl ...
- java idea spring mvc 入门 最起码 我8080跑起来了
IDEA建立Spring MVC Hello World 详细入门教程 https://www.cnblogs.com/wormday/p/8435617.html
随机推荐
- exception java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
1.情景展示 Java 报错信息如下: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 2.原因分析 首先,这是越界异常,但不是数组越 ...
- 第9期《jmeter接口自动化实战》零基础入门!
2019年 第9期<jmeter接口自动化实战>课程,12月6号开学! 上课方式:QQ群视频在线教学 本期上课时间:12月6号-1月18号,每周五.周六晚上20:00-22:00 报名费: ...
- 【技术博客】Git Flow模型管理代码版本
参考GIT版本管理:Git Flow模型,在此基础上加入了自己的理解,增加人员分工和相应代码,并根据本次项目的实际情况进行相应修改. 在本学期的软件工程开发过程中,我们从alpha阶段就使用了git ...
- MyBatis(七):mybatis Java API编程实现增、删、改、查的用法
最近工作中用到了mybatis的Java API方式进行开发,顺便也整理下该功能的用法,接下来会针对基本部分进行学习: 1)Java API处理一对多.多对一的用法: 2)增.删.改.查的用法: 3) ...
- 013 mint-ui
一:概述 1.中文文档 https://mint-ui.github.io/#!/en 2.安装包 二:mt-button的使用 主要是介绍css-component 1.程序 在App.vue下面写 ...
- Linux find命令忽略目录的查找方法
在Linux操作系统中,find命令非常强大,在文件与目录的查找方面可谓无所不至其极,如果能结合xargs命令使得,更是强大无比. 以下来看看find命令忽略目录查找的用法吧. 例1,根据文件属性查找 ...
- VS2017 winform 打包 安装(使用 Microsoft Visual Studio 2017 Installer Project)
Microsoft Visual Studio 2017 Installer Projects SkyRiN发表于Coding+订阅 253 助力数字生态,云产品优惠大促 腾讯云促销,1核1G 99元 ...
- 泡泡一分钟:Collaborative Mapping with Pose Uncertainties using different Radio Frequencies and Communication Modules
张宁 Collaborative Mapping with Pose Uncertainties using different Radio Frequencies and Communication ...
- webshot一款网页快照工具
webshot https://github.com/brenden/node-webshot Webshot provides a simple API for taking webpage scr ...
- ERROR: CAN'T FIND PYTHON EXECUTABLE "PYTHON", YOU CAN SET THE PYTHON ENV VARIABLE.解决办法
错误原因:Node.js 在安装模块的时候报错,缺少python环境. 解决办法: 第一种方式: 安装Python及环境变量配置 一定要安装python2.7的版本 环境变量安装可以参考:http:/ ...