SpringMVC的简介与框架搭建
-
- 1.Spring mvc 的概念
- 2.SpringMVC的框架搭建
为什么学习SpringMVC :
- 我们发现,每当用户发送一个请求,就对应后台一个servlet,如果用户有100个请求,这个时候就需要用户书写100个servlet
- 使用现在servlet进行页面数据接受的时候,我们发现相当的麻烦
- 我们书写的java代码和servlet 之间的耦合太高
1.Spring mvc 的概念
- Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面
- 类似框架 Struts 2 、JFinal
- Springmvc 通过图形可以发现,springmvc就是对servlet进行的封装,实现了java代码和servlet 直接的解耦
2.SpringMVC的框架搭建
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<!--除了jsp之外的所有请求资源-->
<url-pattern>/</url-pattern>
<!-- <url-pattern>*.action</url-pattern>-->
</servlet-mapping>
</web-app>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1>index.jsp</h1>
<img src="img/1.png">
</body>
</html>
<?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:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描注解@Controller-->
<context:component-scan base-package="com.controller"></context:component-scan>
<!--@RequestMapping-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--静态资源的放行-->
<!--mapping:指代的是网络的地址 location:指代的是放行本地的什么资源 -->
<mvc:resources mapping="/img/**" location="/img/"></mvc:resources>
</beans>
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Mycon {
@RequestMapping("demo1")
public String demo1() {
//[1]接受页面数据
//[2]数据处理
System.out.println("进入了demo1控制单元");
//[3]做出响应
return "index.jsp";
}
}
运行结果:
版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: