zoukankan      html  css  js  c++  java
  • SpringMVC版的hello word

    1)、建立maven项目

    2)、添加WEB项目

      1)选中项目,右键添加WEB框架支持

      

      2) 选中WEB框架添加

      

    3)、在poxm.xml中导入相关依赖

    <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.2</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
        </dependencies>

    4)、配置web.xml

    <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:spring-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>encoding</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>utf-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    5)、在src/main/resources下配置spring-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
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <context:component-scan base-package="com.lxy"/>
        <mvc:annotation-driven/>
        <mvc:default-servlet-handler/>
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>

    6)、写HelloController类

    @Controller
    public class HelloController {
        @RequestMapping("hello")
        public String hello(){
            return "forward:/index.jsp";
        }
    }

  • 相关阅读:
    Android : iperf-2.0.4 网络测试工具
    Android : SELinux 简析&修改
    Android : 修改内核源码 and 编译、打包成新的boot.img
    Bluedroid: 音频数据的传输流程
    Bluedroid: 蓝牙协议栈源码剖析
    【Java】 剑指offer(54) 二叉搜索树的第k个结点
    【Java】 剑指offer(53-3) 数组中数值和下标相等的元素
    【Java】 剑指offer(53-2) 0到n-1中缺失的数字
    【Java】 剑指offer(53-1) 数字在排序数组中出现的次数
    【Java】 剑指offer(52) 两个链表的第一个公共结点
  • 原文地址:https://www.cnblogs.com/lxy-java/p/12932824.html
Copyright © 2011-2022 走看看