通过配置文件的方式实现一个简单的HelloWorld。

源码

一、新建项目

1、新建动态web项目

2、命名工程springmvc-01

3、勾选"Generate web.xml deployment descriptor"

4、导入jar包

5、新建springmvc配置文件

6、命名配置文件springmvc-servlet.xml,命名规则:<servlet-name>-servlet.xml

默认路径为:/WEB-INF/springmvc_servlet.xml

7、选择命名空间

8、完成

二、配置文件

1、配置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">
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

2、配置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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <!-- 配置HandlerMapping,根据BeanName找到对应的Controller -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean> <!-- 配置Controller,响应mvc请求 -->
<bean name="/mvc" class="com.zhy.controllers.HelloWorldController"></bean> <!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀 -->
<property name="prefix" value="/WEB-INF/views/"></property>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

三、编写Controller

package com.zhy.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller; public class HelloWorldController implements Controller { /*
* 通过视图解析器(ViewResolver)得到实际的物理视图
* 视图解析器的解析规则:prefix + viewname + suffix
* 结合本实例,视图解析器的解析出来的物理视图为:/WEB-INF/views/helloworld.jsp
* */
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("call controller");
return new ModelAndView("helloworld");
}
}

四、新建jsp页面

1、新建helloworld.jsp,路径:WEB-INF/views/helloworld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

2、新建index.jsp,路径:WEB-INF/index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="mvc">hello world</a>
</body>
</html>

3、完成

四、运行

SpringMVC_HelloWorld_01的更多相关文章

  1. SpringMVC_HelloWorld_02

    一.新建项目 同SpringMVC_HelloWorld_01 二.配置文件 1.配置web.xml <?xml version="1.0" encoding="U ...

  2. SpringMVC_HelloWorld_03

    通过注解的方式实现一个简单的HelloWorld. 源码 一.新建项目 同SpringMVC_HelloWorld_01 不同的是springmvc配置文件的命名和路径,此处为src/springmv ...

随机推荐

  1. 微信 小程序组件 加入购物车全套 one wxml

    <!--pages/shop/shop.wxml--> <view wx:if="{{hasList}}"> <view class="co ...

  2. remote desktop software

    remote desktop software remote desktop https://www.microsoft.com/zh-cn/p/microsoft-remote-desktop/9w ...

  3. hdu 6301 Distinct Values (2018 Multi-University Training Contest 1 1004)

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. 【刷题】BZOJ 4916 神犇和蒟蒻

    Description 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; Input 请你读入一个整数N;1<=N<=1E9,A.B模1E9+7; Output 请你 ...

  5. ubuntu 16.04换源 网易、搜狐、阿里云

    如何更改源可以在软件更新中选择源 使用如下命令更改(修改前先备份): [html] view plain copy print?sudo cp /etc/apt/source.list /etc/ap ...

  6. vim使用入门设置

    分为以下四步. 1,安装vim 2,安装git yum -y install vim git (Fedora/CentOS) /apt-get install vim git (Debian/Ubun ...

  7. [Leetcode]找到出现不同次数的数字(通用解法)

    今天在leetcode上遇到了 137. Single Number II 这道题: 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次.找出那个只出现了一次的元素.(Give ...

  8. python BitTornado P2P分发大文件

    P2P分发大文件思路 1.将软件包生成种子文件 2.通过saltstack将种子文件分发至每台服务器 3.每台服务器进行种子下载 推荐使用Twitter开源的murder.Twitter用它来分发大文 ...

  9. 项目中经常用到的JavaScript方法

    1. js切割字符串 String.split() 注意:此方法与Array.join执行的方法是相反的. 2. js把数组中所有元素放入一个字符串 Array.join()

  10. POJ 3252 Round Number(数位DP)

    Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6983   Accepted: 2384 Des ...