springmvc 学习分享(一)

本片文章的项目Demo

  • 链接地址:

步骤介绍

  • 创建UserController
     
    package com.dl.code.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    /**
     * Created with IntelliJ IDEA.
     * 作者: 代蒙恩
     * 日期: 2021/2/26
     * 时间: 11:23
     * 描述: spring事物学习
     * 内容:
     */
    @Controller
    @RequestMapping("userController")
    public class UserController {
    
        @RequestMapping("login")
        public ModelAndView login(){
            System.out.println("查询数据库中.....");
            //创建一个ModelAndView对象
            ModelAndView modelAndView = new ModelAndView();
            //存放查询的数据  这里是模拟
            modelAndView.addObject("username","代先生");
            //设置跳转的页面
            modelAndView.setViewName("index.jsp");
            //返回ModelAndView对象
            return modelAndView;
        }
    }
    

     

  • spring-mvc.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"
           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/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
           ">
    
        <!-- 配置扫描的包路径 -->
        <context:component-scan base-package="com.dl.code"></context:component-scan>
        <!-- 配置处理起映射器和处理起解析器 -->
        <mvc:annotation-driven></mvc:annotation-driven>
        <!-- 配置视图解析器 -->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!--
                配置前缀信息,这是用来进行页面跳转的时候使用的
                例如设置的跳转页面为:modelAndView.setViewName("index.jsp");
                则实际的跳转路径为:/WEB-INF/jsp/index.jsp
            -->
            <property name="prefix" value="/WEB-INF/jsp/"></property>
        </bean>
    </beans>
  • web.xml文件配置
     

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
    
      <!-- springmvc: 配置前端控制器 -->
      <servlet>
        <servlet-name>dispatcherServlet</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>
      </servlet>
      <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>
    

     

  • jsp测试文件
     

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <body>
    <h2>亲,请点击去登录!</h2>
    <a href="login.jsp">去登录页面</a>
    </body>
    </html>
    <%--
      Created by IntelliJ IDEA.
      作者: 代蒙恩
      日期: 2021/2/26
      时间: 11:28
      描述: springmvc学习
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <a href="userController/login">登录</a>
    </body>
    </html>
    <%--
      Created by IntelliJ IDEA.
      作者: 代蒙恩
      日期: 2021/2/26
      时间: 11:14
      描述: springmvc学习
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        用户:${username}
    </body>
    </html>

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值