当前位置: 移动技术网 > IT编程>开发语言>Java > Spring MVC HelloWorld入门及运行机制 (一)

Spring MVC HelloWorld入门及运行机制 (一)

2018年11月07日  | 移动技术网IT编程  | 我要评论

介绍

springmvc是一款web mvc框架。 它跟struts框架类似,是目前主流的web mvc框架之一。

文章通过实例来介绍springmvc的入门知识。

目录

 

实例

maven依赖springmvc jar:

<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-webmvc</artifactid>
<version>5.1.1.release</version>
</dependency>

项目结构路径:

一、配置web.xml:

<?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">
    
    <!-- 配置请求总控器,由spring-webmvc提供,它负责接收所有客户端的请求
         ,然后通过解析url并交给后端控制器(controller)进行请求处理-->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
        <!-- springmvc默认会从web-inf下查找名为
        [servlet-name]-servlet.xml的配置文件,
        也可以通过init-param重新指定配置文件的路径-->
        <init-param>
            <param-name>contextconfiglocation</param-name>
            <!-- 指定从classpath中查找 -->
            <param-value>classpath:dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

二、配置dispatcher-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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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">

    <!-- 启用注解并扫描 -->
    <context:component-scan base-package="edu.nf.ch01.controller"/>
    <!-- 启用mvc注解驱动-->
    <!-- 这个注解驱动注册了requestmappinghandlermapping(请求映射处理器)
    和一个requestmappinghandleradapter(请求映射适配器),同时启用了
    @requestbody,@responsebody注解以及表单验证的等功能-->
    <mvc:annotation-driven/>

    <!-- 配置视图解析器,spring有很多种视图解析器,不同的解析器用于解析成不同的视图对象
         其中有一个叫做internalresourceviewresolver(内部资源解析器)
         ,用于解析内部的jsp资源-->
    <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
        <!-- 给这个视图解析器注入两个属性 -->
        <!-- 指定资源目录的前缀 -->
        <property name="prefix" value="/web-inf/jsp/"/>
        <!-- 指定资源的后缀-->
        <property name="suffix" value=".jsp"/>
        <!-- 这个解析器默认解析的视图对象为internalresourceview,
         如果需要在jsp中使用jstl标签库,那么可以设置为jstlview,
         它是internalresourceview的子类-->
        <!--<property name="viewclass" value="org.springframework.web.servlet.view.jstlview"/>-->
    </bean>
</beans>

三、控制器controller类:

package edu.nf.ch01.controller;

import org.springframework.context.annotation.scope;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.servlet.modelandview;

/**
 * @author wangl
 * @date 2018/10/26
 * 后端控制器
 */
@controller
@scope("prototype")
public class hellocontroller {

    /**
     * 通过@requestmapping来映射请求的url
     * @return
     */
    @requestmapping("/hello")
    public modelandview hello(){
        system.out.println("hello spring");
        modelandview mv = new modelandview("index");
        return mv;
    }
}

四、请求转发的页面

<%--
  created by intellij idea.
  user: wangl
  date: 2018/10/26
  time: 10:50
  to change this template use file | settings | file templates.
--%>
<%@ page contenttype="text/html;charset=utf-8" language="java" %>
<html>
<head>
    <title>title</title>
</head>
<body>
  <h1>welcome!</h1>
</body>
</html>

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网