zoukankan      html  css  js  c++  java
  • Spring MVC 之 Hello World

    1.新建一个动态web项目
    2.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">
    <display-name>springMVC</display-name>
     
    <!-- 配置DispatchcerServlet -->
    <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--
    配置Spring mvc下的配置文件的位置和名称
    也可以使用:默认的配置 路径为 WEB-INF/<servlet-name>-service.xml
    -->
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!-- 加载时被创建 -->
    <load-on-startup>1</load-on-startup>
    </servlet>
     
    <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
     
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>
     
    3.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:mvc="http://www.springframework.org/schema/mvc"
    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">
     
    <!-- 配置自动扫描包 @Controller @RequestMapping -->
    <context:component-scan base-package="com.sguigu.springmvc.handlers" />
     
    <!-- 配置视图解析器 -->
    <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    </bean>
    </beans>
     
    4.controller编写
    package com.sguigu.springmvc.handlers;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
     
    @Controller
    public class HelloWorld {
     
    /**
    * @description hello world
    * @return String 返回类型
    * @author hxt
    */
    @RequestMapping("/helloworld")
    public String hello(){
    System.out.println("-----success---------");
    return "helloworld";
    }
    }
     
     
  • 相关阅读:
    C#怎样保证弹出窗体是唯一并居中显示
    1、怎样设置C#OpenFileDialog(文件选择窗体)的指定路径、文件格式等属性(设置打开默认路径、文件格式、窗体显示文本)
    FTP在CentOS上安装与使用
    nano在CentOS上的安装和使用
    CentOS 7 安装php5.6,Nginx,Memcached环境及配置
    PhpStorm 2017.1安装及破解过程
    在唯一密钥属性“name”设置为“ExtensionlessUrlHandler-Integrated-4.0”时,无法添加类型为“add”的重复集合项
    获取含有字符串数组里元素的数据,并批量删除
    如何去掉browserLinkSignalR
    使用VS2015开发asp程序让IIS express 允许的父路径的方法
  • 原文地址:https://www.cnblogs.com/qypx520/p/6222505.html
Copyright © 2011-2022 走看看