SpringMVC_HelloWorld_01
通过配置文件的方式实现一个简单的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的更多相关文章
- SpringMVC_HelloWorld_02
一.新建项目 同SpringMVC_HelloWorld_01 二.配置文件 1.配置web.xml <?xml version="1.0" encoding="U ...
- SpringMVC_HelloWorld_03
通过注解的方式实现一个简单的HelloWorld. 源码 一.新建项目 同SpringMVC_HelloWorld_01 不同的是springmvc配置文件的命名和路径,此处为src/springmv ...
随机推荐
- 云平台项目--学习经验--打包压缩工具requirejs
requirejs是一个JavaScript模块加载器.适合在浏览器中国使用,也可以在其他脚本环境使用,它鼓励了代码的模块化.使用RequireJS加载模块化脚本将提高代码的加载速度和质量.如何加载R ...
- Beta版本冲刺(五)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- C# 项目迁移 Microsoft.VisualStudio.Tools.Office.BuildTasks 生成解决方法报错:请确认 <UsingTask> 声明正确,该程序集及其所有依赖项都可用
问题定位: 1.在Server2003上使用vs2010开发的项目,移到Win8上,同样使用vs2010打开.在生成解决方案的时候有如下报错: 未能从程序集 Microsoft.VisualStudi ...
- 六大Web负载均衡原理与实现
还有个姊妹篇也可以参考这个文章:LVS(Linus Virtual Server):三种负载均衡方式比较+另三种负载均衡方式, LVS 实现了负载均衡,NAT,DR,TUN zookeeper使用ZA ...
- 深入理解ajax系列第八篇——表单提交
前面的话 在以前,网站的用户与后端交互的主要方式是通过HTML表单的使用.表单的引入在1993年,由于其简单性和易用性,直到电子商务出现之前一直保持着重要位置.理解表单提交,对于更深入地理解ajax是 ...
- Flash 解题报告
Flash Description 给你一颗树,需要把每个点染色,每个点染色时间为\(t_i\),要求同时染色的点的集合为树的独立集,最小化染色结束时间之和. 其实题面蛮有趣的♂ HINT \(n\l ...
- 界面编程之QT的Socket通信20180730
/*******************************************************************************************/ 一.linu ...
- Linux上防火墙开放对应的端口
在Linux上防火墙开放对应的端口的命令如下: 方式一: [root@localhost sbin]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACC ...
- 20181108 Apache Commons Lang
工具类 org.apache.commons.lang3 AnnotationUtils ArchUtils ArrayUtils BooleanUtils CharSetUtils CharUtil ...
- Hadoop生态圈-Azkaban实现hive脚本执行
Hadoop生态圈-Azkaban实现hive脚本执行 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客中在HDFS分布式系统取的数据,而这个数据的是有之前我通过MapRed ...