SpringMvc入门教程
1.新建demo4 web项目, 导入spring包(使用的是spring4.2)
2.修改WEB-INF下的WEB.XML内容为
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet> <servlet-name>dispatch</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatch</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
View Code
3.在WEB-INF下的新建spring-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" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"><context:component-scan base-package="com"></context:component-scan> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> </beans>
View Code
4.在SRC下新建 包 com.game.controller
5.在该包下新建类
package com.game.controller;import java.io.IOException; import java.util.Date;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;@Controller public class Helloworld {@RequestMapping("/helloworlda") //此处控制浏览器里访问路径 具体为:/SpringDemo/helloworldpublic void helloWorld(HttpServletRequest request, HttpServletResponse response) throws IOException {//输出字符串response.getWriter().append("hello world--a");}@RequestMapping("home") public ModelAndView home(){ ModelAndView mv=new ModelAndView();mv.addObject("name","xiaoxiao");mv.getModel().put("age", "111110");return mv;} }
View Code
6.在web-inf下新建名为jsp的文件夹,并新建home.jsP文件,内容为
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'home.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>This is my JSP page. ${age} ==${name} }<br></body> </html>
View Code
7.打开地址 http://localhost:8080/demo4/home.do
SpringMvc入门教程相关推荐
- SpringMVC 入门教程
SpringMVC 入门教程 1.参考资料 2020年IDEA版黑马Java就业班-进阶篇(Mybatis.Spring.SpringMVC.Maven.springboot和项目等等 项目地址:On ...
- springmvc教程--快速入门教程
springmvc教程系列: springmvc史上最好教程(3) springmvc史上最好教程(2) 一. SpringMVC架构 1.1. Spring web mvc介绍 Spring web ...
- 【动力节点】springmvc框架入门教程-从入门到精通
目录 一.Springmvc入门 1.Springmvc是什么 2.Springmvc处理流程 3.入门程序 二.springMVC架构 1.springMVC的架构图 2.架构流程 3.spring ...
- SpringMVC 基础教程 简单入门实例
spring MVC 入门教程二: 一个简单的入门实例教程 该实例的源码和实例中的jar 源码:http://download.csdn.net/detail/swingpyzf/5348563 所需 ...
- SpringMVC入门笔记
SpringMVC入门笔记 1. 简介 Spring MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架 ,是Spring系开源项目中的一个,和IoC配合使用.通过 ...
- ijidea搭建springMVC入门程序,配置TomCat
ijidea搭建springMVC入门程序,适用于超级新手.保姆教程 1. 2. 3. 4.输入可以更快创建项目,不需要去下载 archetypeCatalog internal 5.此时的目录结构: ...
- 零基础SSM入门教程(0)--教程背景与目录
点此查看 零基础JavaWeb全栈文章目录及源码下载 1. 背景 本教程的出发点是写一个比较条理,比较全面的SSM系列教程,首先造福自己.理顺自己的知识体系,然后也造福大众,因为网上现存的资源相对比较 ...
- Spring 入门教程
Spring 入门教程 1.参考资料 尚硅谷-Spring5框架最新版教程(idea版) 雷丰阳spring.springmvc.mybatis.spring一站式学习 项目地址:Oneby / sp ...
- SpringBoot入门教程(转)
SpringBoot入门教程 注:此文章转载于GitHub,但是原文章图片加载出错,因此我整理了一下,并非本人原创,在此感谢原作者! 附上原文视频课地址供大家学习: www.bilibili.com/ ...
最新文章
- mysql 1594_【MySQL】复制1594错误(从库relaylog损坏)
- 读文件夹下所有文件,并提取中文字符
- Linux vi/vim使用方法 总结
- 阿里云故障演练平台获得可信云最高等级认证,为企业数字韧性能力保驾护航
- script与scriptreplay用法
- 使用Stream.peek在Java Streams内部进行窥视
- linux安装软件方法汇总
- win32 c语言编程,win32环境C语言实现最基本的DLL编写及调用实例,测试通过[原]
- 算法:把排好序的链表转换为二叉排序树Convert Sorted List to Binary Search Tree
- 六、量子纠错码的构成法
- centos7救援模式--rescue模式
- c语言程序答案PDF,C语言程序设计答案.pdf
- ctcpejmu单词_高分子英语单词
- 数电发票(全电发票)时代如何查验发票?
- 博士申请 | 美国密歇根州立大学ACTION Lab招收CV/ML方向全奖博士生
- 我们是如何走到今天的?
- 基于hi3531、ffmpeg、x264的h264压缩
- 进程间通信的方式及原理
- python名片管理器实验报告_名片管理系统——实验报告.doc
- 张晓飞ajax聊天室,张晓飞欧阳乐做客信息逸站作专题报告