SpringMVC初学--hello world

SpringMVC初学

一.HelloWorld

项目结构:

1. 加入的jar包

2. 配置web.xml文件

(1) 配置DispatcherSevlet

<?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">

    <!--配置DispatcherServlet-->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置DispatcherServlet的一个初始化参数:配置SpringVC配置文件的位置和名称-->
        <init-param>
            <!--名称-->
            <param-name>contextConfigLocation</param-name>
            <!--位置-->
            <param-value>classpath:spingmvc.xml</param-value>
        </init-param>
    </servlet>

    <!--配置映射文件-->
    <servlet-mapping>
        <!--该名称与配置DispatcherServlet的Servlet下的<servlet-name>一致 -->
        <servlet-name>springDispatcherServlet</servlet-name>
        <!-- 路径 :当前表示所有路径-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

3. 新建控制器类—HelloWorld.java

package com.archer.helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 控制器
 * @author Archer
 * @date 2018-12-29 10:08
 **/
@Controller
public class HelloWorld {

    /**
     * 映射路径
     * 1.使用 @RequestMapping来注解请求的url    /hello表示url
     * 2. 返回值会通过试图解析器解析为实际的物理视图,对于InternalResourceViewResolver视图解析器,会有如下解析:
     * /WEN-INF/views/success.jsp
     *
     * prefix:/WEN-INF/views/
     * suffix: .jsp
     * @return
     */
    @RequestMapping("/hello")
    public String hello(){
        System.out.println("Hello World!");
        return "success";
    }
}

 

 

4. springmvc配置文件

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

<!--springmvc的配置文件-->

<!--配置自定义的扫描包-->
<context:component-scan base-package="com.archer.helloworld"></context:component-scan>

<!--配置视图解析器:如何把handler方法返回值解析为实际的物理视图-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值