zoukankan      html  css  js  c++  java
  • Spring Mvc 入门Demo

    1、web.xml配置

    <?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_3_1.xsd"
             version="3.1">
        <context-param>   --Spring Controller 配置
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> 
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <servlet>  --Spring mvc 默认调度
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>sh</servlet-name>
            <url-pattern>/servlet/sh</url-pattern>
        </servlet-mapping>
    </web-app>

    load-on-startup表示启动容器时初始化该Servlet;

    url-pattern表示哪些请求交给Spring Web MVC处理,配置Spring的配置文件,默认DispatcherServlet会加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件.

    2、dispatcher-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        <context:component-scan base-package="com.javahash.spring.controller"></context:component-scan>   --配置扫描包
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value=""></property>     --路径前缀
            <property name="suffix" value=".jsp"></property> --路径后缀
        </bean>
    </beans>

    3、Controller 代码

    package com.javahash.spring.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    import java.util.logging.Logger;
    
    /**
     * Created by Administrator on 2014/11/8.
     */
    @Controller
    public class HelloWorldController {
    
        @RequestMapping("/hello")
        public String hello(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
            model.addAttribute("name", name);
            return "helloworld";
        }
    }
  • 相关阅读:
    【GIT-精讲】从零玩转Git-基础理论
    【fmjava】 面试题突击训练-Java基础语法篇01
    【笔记】springSecurity-OAuth2.0-授权模式演示
    【难受】SpirngCloud-Alibaba-nacos跨服务器访问接口的问题
    Python编程题汇总(持续更新中……)
    Python编程题14--随机分配礼物
    Python编程题13--判断两个升序列表,其中一个是另外一个的子集
    Python编程题12--列表中比前面元素都大,比后面元素都小的数
    Python编程题11--找出100以内的质数
    Python编程题10--找出和为N的两个数
  • 原文地址:https://www.cnblogs.com/tyb1222/p/4143778.html
Copyright © 2011-2022 走看看