zoukankan      html  css  js  c++  java
  • springMVC简单示例

       1、新建web工程

       2、引入springframework架包

       

       

      3、配置文件

          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">
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>

       springmvc-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"
           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.xsd">
    
        <context:component-scan base-package="com.zay"></context:component-scan>
    
        <!-- 配置视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
    </beans>

       4、源代码

    package com.zay;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    /**
     * Created by zadom on 2016/11/27.
     */
    @Controller
    @RequestMapping("/welcome")
    public class HelloController {
        @RequestMapping(value="/hello",method = RequestMethod.GET)
        public String prinWelcome(ModelMap model){
            model.addAttribute("message","hello!!!");
            return "hello";   //返回页面模板的名字,这个名字在mvc-dispatcher-servlet.xml中可以知道其前缀和后缀,并最终得到其jsp文件
        }
    }

       5、视图文件

            hello.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    ${message}
    </body>
    </html>
  • 相关阅读:
    微信公众号开发之用户地理位置坐标转百度坐标
    PHP变量入门教程(1)基础
    【很变态】PHP类实例化对象竟然可以访问类的“静态(static)方法”!!!
    【转】记录PHP、MySQL在高并发场景下产生的一次事故
    PHP返回32位与16位的md5加密值
    PhpStorm 8.x/9.x 快捷键设置/个性化设置,如何多项目共存?如何更换主题?
    Linux设置Memcached开机启动
    【荐】MongoDB基本命令大全
    【荐】PHP操作MongoDB GridFS 存储文件,如图片文件
    Shell入门教程:流程控制(7)break和continue
  • 原文地址:https://www.cnblogs.com/zadomn0920/p/6111928.html
Copyright © 2011-2022 走看看