标签:内存 继承 pat 案例 配置 val 配置步骤 rtu 2.4
springmvc的简介
springmvc也叫 spring Web(网络) mvc 属于表现层(UI)的框架。是spring框架的一部分。
springmvc 第一个入门案例
1.导jar包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId> org.aspectj</groupId >
<artifactId> aspectjweaver</artifactId >
<version> 1.8.7</version >
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<!--SpringWeb-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
<!--ServletAPI-->
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
配置步骤
1.在web.xml中配置前端控制器。 必须配!
2.处理器映射器(HandlerMapping)可以不配,系统给默认值。
3.处理器适配器(HandlerAdaptor)
4.处理器
5.视图解析器(ViewResolver)
一:前端控制器:本身就是一个servlet
<!-- 前端控制器-->
<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:springmvc.xml</param-value>
<!-- tomcat一启动,就将servlet对象创建为完好保存到内存中-->
<load-on-startup>1</load-on-startup>
</servlet>
<spring-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern> / 拦截所有请求。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<bean id="/hello.do" class="cn.happy.Controller.FirstController"></bean>
</beans>
二:控制器 claass implements Controller 必须继承这个Controller。
public class FirstController implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv= new ModelAndView(); Modelandview(模板和试图)
mv.setViewName("/WEB-INF/index.jsp");
return mv;
}
web-inf层下的index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>title</title>
</head>
<body>
<h1>hello spring mvc 啦啦啦</h1>
</body>
</html>
标签:内存 继承 pat 案例 配置 val 配置步骤 rtu 2.4
原文地址:http://www.cnblogs.com/wangdan123/p/7359207.html