zoukankan      html  css  js  c++  java
  • 第一个SpringMVC程序(注解实现)

    第一个SpringMVC程序(注解实现)

    Spring MVC 的核心包是:

    • spring-web
    • spring-webmvc

    目录结构:

    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_4_0.xsd"
             version="4.0">
        <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-mvc.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>
    </web-app>
    
    • 在上述web.xml文件中主要配置了<servlet><servlet-mapping>
      • 配置了前端控制器DispatcherServlet
      • 绑定了springmvc配置文件的位置
      • load-on-startup元素中的 1 表示容器(Tomcat)在启动时立即加载这个 DispatcherServlet
      • url-pattern 元素的 /,会对所有 URL 拦截(不包括 jsp / html 等),并交由 DispatcherServlet 处理

    2、编写Spring mvc的配置文件

    <?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.xu.controller"/><!--自动包扫描-->
        <mvc:default-servlet-handler/><!--让spring不处理请求静态资源的请求-->
    
        <mvc:annotation-driven/><!--代替了:HandlerMapping  和  HandlerAdapter-->
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/><!--前缀-->
            <property name="suffix" value=".jsp"/><!--后缀-->
            <!--到时候我们直接返回逻辑视图名就ok了!-->
        </bean>
    
    </beans>
    

    3、编写视图(hello.jsp)

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ page isELIgnored="false" %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>第一个MVC程序(注解实现)</title>
    </head>
    <body>
    <h1>${msg}</h1>
    </body>
    </html>
    

    4、编写Controller

    package com.xu.controller;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloController {
    
        @RequestMapping("/hello") //括号里写请求的路径
        public String hellomvc1(Model model ){
            model.addAttribute("msg","第一个Spring-MVC程序(注解实现)");
            return "hello"; //直接返回视图名
        }
    

    5、测试

  • 相关阅读:
    GridView自定义分页
    intro
    ListView和DataPager初试
    在DataGrid中,如何录入数量及单价的时候自动算出金额
    常用正则表达式
    ASP.NET中基类页的设计和使用
    Asp.net实现无刷新检测用户名
    在asp.net2.0中使用存储过程
    .NET中的抽象工厂
    用C#生成随机中文汉字验证码的基本原理 [转]
  • 原文地址:https://www.cnblogs.com/whitespaces/p/12450348.html
Copyright © 2011-2022 走看看