SpringMVC初学之HelloWorld

SpringMVC是一个mvc框架,用来简化基于mvc架构的web应用开发

五大组件:

DispatcherServlet     控制器 处理请求 依据HandlerMapping的配置调用相应的模型处理

HandlerMapping       请求路径与模型的对应关系

Controller                 负责处理业务逻辑

ModelAndView         封装了处理结果,将处理结果封装成该对象,除了数据之外,还可以是一个视图

VIewResolver           视图解析器

流程图:(图中的DispatcherServlet拼写有错误!大家不要在意)

流程步骤:

1.DispatcherServlet收到请求之后,依据HandlaarHandlerMapping的配置,调用相应的 Controller来处理

2. Controller将处理结果封装成ModelAndView对象,然后返回给DispatcherServlet

3. DispatcherServlet依据ViewResViewResolver的解析,调用相应的视图对象,比如(xxx.jsp)

 

开发步骤:

1.导包    (pom.xml)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.txd</groupId>
  <artifactId>SpringMVC_HelloWorld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
    <dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.2.RELEASE</version>
</dependency>
</dependencies>
</project>

2.添加空的spring配置文件    (spring-web.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/aop
      http://www.springframework.org/schema/aop/spring-aop-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/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
      ">

      </beans>

3. 配置DispatcherServlet    (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_2_5.xsd" version="2.5">
<servlet>
	<servlet-name>helloworld</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<!-- 配置Spring文件的路径 -->
	<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>helloworld</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

4.写Controller 处理器

实现了Controller接口 这个接口中有方法需要去实现,返回一个ModelAndView对象

package controller;

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 HelloController implements Controller{

	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("Controller()");
		/*
		 * ModelAndView有两个常用的构造器
		 * 1.ModelAndView(String s)    s 是视图名
		 * 2.	ModelAndView(String,Map map)   map是封装后的处理的数据
		 */
		return new ModelAndView("hello");
	}
	
}

5. 需要在spring配置文件中配置HandlerMapping和ViewResolver

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.0.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
      ">

	<!-- 
			配置HandlerMapping 
			如果请求是hello 就   controller.HelloController
	-->
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop  key="/hello">helloController</prop>
			</props>
		</property>
	</bean>
	<bean id="helloController" class="controller.HelloController" > </bean>



	<!-- 配置视图解析器 (前缀/后缀) -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>

HandlerMapping配置了如果请求是/hello 则调用controller.HelloController这个模型

ViewResolver 配置了返回的视图的前缀后缀

6. 在WEB-INF路径下添加文件  hello.jsp     

7. 分析:

如果我发送http://localhost:8080/hellomvc/hello这个请求

配置文件中的 HandlerMapping识别到hello,调用了Controller这个模型,这个模型返回的数据是ModelAndView类型的对象,对象中是一个视图hello

视图解析器给视图路径拼接了前缀和后缀,最终形成了    /WEB-INF/hello.jsp    这个路径,正好我们的该路径下有这个hello.jsp,如图:

所以返回给前台这个jsp页面!

 

好的,mvc架构helloworld到此结束!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值