美文网首页
sprint-mvc-1-hellowold

sprint-mvc-1-hellowold

作者: liangxifeng833 | 来源:发表于2018-01-22 20:26 被阅读21次

    一.新建web项目,目录结构如下

    mvc-1.jpg

    二.配置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_3_0.xsd"
        id="WebApp_ID" version="3.0">
        <!-- 配置DispatcherServlet -->
        <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
        <servlet>
            <servlet-name>springDispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--  配置DispatcherServlet的一个初始化参数,配置springMVC配置文件的位置和名称 -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            <!-- 当前web应用被加载的时候被创建,而不是被请求的时候创建 -->
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <!-- Map all requests to the DispatcherServlet for handling -->
        <servlet-mapping>
            <servlet-name>springDispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    

    三.配置springmvc.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-4.3.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
    
        <!-- 配置自动扫描的包 -->
        <context:component-scan base-package=" lxf.springmvc"></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>
    

    四.新建HelloWorld控制器

    package lxf.springmvc.handlers;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * 请求处理器
     * @author lxf
     */
    //@RequestMapping注解修饰类
    @RequestMapping("/helloClass")
    @Controller
    public class HelloWorld {
        /**
         * 1.使用@RequestMapping注解来映射请求的url,修饰方法
         * 2.返回值会通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver解析器,会做如下的解析:
         *   通过 prefix + returanval + 后缀,这样的方式得到实际的物理视图,然后做转发操作
         *         /WEB-INF/views/success.jsp
         * @return
         */
        @RequestMapping("/helloMethod")
        public String hello()
        {
            System.out.println("Hellow world!");
            return "success";
        }
    }
    

    以上 @RequestMapping 注解既可以 修饰类 ,也可以修饰 方法

    五.新建view,success.jsp

    <h4>Success Page!</h4>
    

    六.访问测试:

    • 如果@RequestMapping不修饰类的时候访问如下:
    http://localhost:8081/spring-mvc-helloworld/helloMethod
    
    • 如果 @RequestMapping修饰类的时候访问如下:
    http://localhost:8081/spring-mvc-helloworld/helloClass/helloMethod
    

    点击查看演示代码

    相关文章

      网友评论

          本文标题:sprint-mvc-1-hellowold

          本文链接:https://www.haomeiwen.com/subject/upjplxtx.html