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";
    }
    }
     
     
  • 相关阅读:
    Scrapy 扩展中间件: 针对特定响应状态码,使用代理重新请求
    Scrapy 扩展中间件: 同步/异步提交批量 item 到 MySQL
    Scrapy 隐含 bug: 强制关闭爬虫后从 requests.queue 读取的已保存 request 数量可能有误
    Scrapyd 改进第二步: Web Interface 添加 STOP 和 START 超链接, 一键调用 Scrapyd API
    简单示例理解神闭包
    ejs 模板使用方法
    我使用的开源组件汇总,以备学习使用
    了不起的Node.js--之五 TCP连接
    Windows7下Java运行时环境搭建
    了不起的Node.js--之四
  • 原文地址:https://www.cnblogs.com/qypx520/p/6222505.html
Copyright © 2011-2022 走看看