Spring MVC系列[1]—— HelloWorld
1.导入jar包
ioc
mvc
复制spring-mvc.xml到src目录下。
2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3.spring-mvc.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 开启注解扫描 -->
<context:component-scan base-package="com.java"/> <!-- 开启mvc注解扫描 -->
<mvc:annotation-driven/> <!-- 定义视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean> </beans>
4.建立Controller类
package com.java.springmvc; 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("/hello.do")
public ModelAndView hello(
HttpServletRequest request,
HttpServletResponse response)
throws Exception{
System.out.println("hello world");
return new ModelAndView("jsp/hello");
} @RequestMapping("say.do")
public String say(){ System.out.println("hello contorller");
return "jsp/hello";
} }
Controller类
5.在WEB-INF目录下建立jsp目录,建立新的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 'hello.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>
Hello World! <br>
</body>
</html>
6.总结
①在xml中指定src目录下的文件用classpath。
②WEB-INF是安全目录。所谓「安全目录」,就是客户端不能访问,但是服务器可以访问的目录。
③创建bean时,容易出现的错误:class=“.class”.应该是class=“”(不带.class)。
④Controller返回应该使用相对路径而不是绝对路径。
⑤MVC基本概念
M model
业务逻辑和数据:实体,dao,service
V view
jsp,asp
C controller
是沟通Controller和view的桥梁
⑥MVC流程:
request --> HandlerMapping --> controller --> ModelAndView --> VewResolver --> View --> response
⑦DispatcherServlet四大组件:
(1).HandlerMapping
(2).controller
(3).ModelAndView
(4).ViewResolver
Spring MVC系列[1]—— HelloWorld的更多相关文章
- Spring mvc系列一之 Spring mvc简单配置
Spring mvc系列一之 Spring mvc简单配置-引用 Spring MVC做为SpringFrameWork的后续产品,Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块 ...
- 【Spring MVC系列】--(4)返回JSON
[Spring MVC系列]--(4)返回JSON 摘要:本文主要介绍如何在控制器中将数据生成JSON格式并返回 1.导入包 (1)spring mvc 3.0不需要任何其他配置,添加一个jackso ...
- 基于XML配置的Spring MVC 简单的HelloWorld实例应用
1.1 问题 使用Spring Web MVC构建helloworld Web应用案例. 1.2 方案 解决本案例的方案如下: 1. 创建Web工程,导入Spring Web MVC相关开发包. Sp ...
- spring MVC 初探 (HelloWorld)
1.使用spring MVC 需要导入相关jar包 2.web.xml 启用spring MVC <servlet> <servlet-name>spring3mvc</ ...
- 0000 - Spring MVC 原理以及helloworld
1.概述 Spring MVC是目前最好的实现MVC设计模式的框架,是Spring框架的一个分支产品.以Spring IOC容器为基础,并利用容易的特性来简化它的配置.Spring MVC相当于Spr ...
- 我看Spring MVC系列(一)
1.Spring MVC是什么: Spring MVC:Spring框架提供了构建Web应用程序的全功能MVC模块. 2.Spring helloWorld应用(基于Spring 4.2) 1.添加S ...
- Spring MVC系列之模型绑定(SpringBoot)(七)
前言 上一节我们在SpringBoot中启用了Spring MVC最终输出了HelloWorld,本节我们来讲讲Spring MVC中的模型绑定,这个名称来源于.NET或.NET Core,不知是否恰 ...
- Spring MVC系列之JDBC Demo(SpringBoot)(七)
前言 前面我们了解了Spring MVC的基本使用,其实和.NET或.NET Core MVC无异,只是语法不同而已罢了,本节我们将和和数据库打交道,从最基础的JDBC讲解起,文中若有错误之处,还望指 ...
- 基于注解配置的Spring MVC 简单的HelloWorld实例应用
2.1 问题 使用注解的方式重构helloworld应用案例. 2.2 方案 1. @RequestMapping注解应用 @RequestMapping可以用在类定义和方法定义上,它标明这个类或方法 ...
随机推荐
- World is Exploding
题意: 给出一个长为n的序列A,问有多少四元组(a, b, c, d)满足$a \ne b \ne c \ne d, 1 \leq a < b \leq n, 1 \leq c < d \ ...
- Matrix Recurrence
给定矩阵$A,B$,且有 $$f(0) = A ,f(i) =B * \prod_{i=w(i)}^{i-1}f(i)$$ 求f(n) 其中,当w(i)单增时,可以做到$O(n*m^3)$,注意要优化 ...
- 3.4-3.6 依据业务需求分析HBase的表设计
一.依据[话单]查询需求分析HBase的表设计 1.分析 用户需要进行实时的查询,那么这些数据是放在HBase当中的,每个客户每天接打电话至少20个左右,而通信公司拥有很多用户,每天产生的数据都是上亿 ...
- c++中stl----vector
1 vector是啥玩意 (1)可以使用下标访问个别的元素 (2)迭代器可以按照不同的方式遍历 (3)可以在容器的末尾增加或者删除元素 2 容器大小和容器的容量区别 (1)大小是元素的个数,容量是分配 ...
- python数据分析笔记中panda(3)
1 按照空格将一列的内容分为两列 from pandas import Series; from pandas import DataFrame; from pandas import read_cs ...
- Flutter实战视频-移动电商-06.Dio基础_Get请求和动态组件协作
博客地址: https://jspang.com/post/FlutterShop.html#toc-0ee 编写页面代码 创建动态组件HomePage,原来的代码是静态的我们这里就去掉就可以了. 然 ...
- 3-3Java程序的结构
这是类的定义 这是主方法的定义 类里面包含一个主方法,或者是主方法嵌套到我们的类里面 大括号要特别注意,通过大括号我们可以看到类和主方法的包含关系 class后面一定是跟的类的名字
- javascript runat server
<script runat="server"> protected void SubmitBtn_Click(object sender, EventArgs e) { ...
- Spring Boot 学习系列(04)—分而治之,多module打包
此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 明确功能,各司其职 在一个结构清晰的项目中,一个没有module划分的结构显然不是最佳实践.有人会说可以在同 ...
- laravel C层 (控制器)
<?php namespace App\Http\Controllers; use App\Index; use App\Http\Controllers\Controller; class I ...