锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

SpringMVC:第一个程序(动力)

时间:2023-06-04 10:07:00 mgs203磁性接近传感器

首先配置DispatcherServlet:

自定义读取配置文件的位置

pom.xml:引入依赖

     4.0.0    com.beijingnode   ch01-hellow-springmvc   1.0-SNAPSHOT   war          UTF-8     1.8     1.8                   junit       junit       4.11       test                  javax.servlet       javax.servlet-api       3.1.0       provided                   org.springframework       spring-webmvc       5.2.5.RELEASE                                  maven-compiler-plugin         3.1                    1.8           1.8                          

web.xml:初始化配置

声明springmvc核心对象,声明servlet-mapping

                 myweb         org.springframework.web.servlet.DispatcherServlet                                     contextConfigLocation                          classpath:springmvc.xml                    1                      myweb         *.do      

创建springmvc配置文件:说明扫描

springmvc.xml:

         

创建控制类:MyController:创建控方法

package com.bjpowernode.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/*
@Controller:创建处理器对象,对象放在springmvc容器中
位置:在类的上面  和spring中的@Service,@Component一样

能处理请求的都是控制器(处理器):MyController能处理请求,叫做后端控制器
 */
@Controller
public class MyController {
    /*
    处理用户的请求,springmvc中是使用方法来处理的:方法是自定义的,可以有多种返回值,多种参参数

    准备处理doSome方法处理some.do请求
    @RequestMappper:请求映射,作用是把一个请求地址和一个方法绑定在一起,一个请求指定一个方法处理
       属性:value是一个String,表示请求的url地址(do.some)
             value的值必须是唯一 的,不能重复使用,在使用时,推荐地址以 /开头
       使用RequestMapping修饰的方法可以处理请求的类似Servlet中的doGet doPost

       位置:在方法的上面或者在类的上面

       返回值:ModelAndView:表示本次请求的处理结果
             Model:数据,请求处理完成后,要显示给用户的数据
             View:视图,比如jsp等等
     */
    @RequestMapping(value = "/some.do")
    public ModelAndView doSome(){ //doSome相当于doGet()---调用Service请求处理
        System.out.println("调用Sevice后:");//这里先不调用,简化处理
        ModelAndView mv=new ModelAndView();

        //添加数据,框架在请求的最后把数据放入到request作用域。
        //request.setAttribute("msg","欢迎使用mvc开发")
        mv.addObject("mgs","欢迎使用mvc开发");
        mv.addObject("fun","执行的是dosome方法");

        //指定视图的完整路径
        //框架对视图执行的forword操作,request.getRequestDisPatcher("/show.jsp").forward(..)
        mv.setViewName("/show.jsp");

        //返回mv
        return mv;

    }
}

运行服务器访问页面:index.jsp,发起*.do请求

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 17:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>


    Title
    


  

hellow word

发起some.do请求

请求处理,跳转的页面:show.jsp

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 19:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>


    Title
    


 

show.jsp,从request作用域中获取数据

msg数据:${msg}

fun数据:${fun}

运行服务器:

 

 

为了防止用户直接访问:webapp下的jsp页面,可以进行下面操作

 WEB-INF目录下的内容是不可直接访问的,对用户是不可见的,用户是无权访问的

在WEB-INF创建一个新的子目录:view,之后用户在进行访问会报404

创建目录之后,控制器中的跳转路径了也需要进行更改:

mv.setViewName("/WEB-INF/view/show.jsp"

配置视图解析器:

springmvc.xml:





    


    
        
        
        
        
    

MyController:

package com.bjpowernode.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/*
@Controller:创建处理器对象,对象放在springmvc容器中
位置:在类的上面  和spring中的@Service,@Component一样

能处理请求的都是控制器(处理器):MyController能处理请求,叫做后端控制器
 */
@Controller
public class MyController {
    /*
    处理用户的请求,springmvc中是使用方法来处理的:方法是自定义的,可以有多种返回值,多种参参数

    准备处理doSome方法处理some.do请求
    @RequestMappper:请求映射,作用是把一个请求地址和一个方法绑定在一起,一个请求指定一个方法处理
       属性:value是一个String,表示请求的url地址(do.some)
             value的值必须是唯一 的,不能重复使用,在使用时,推荐地址以 /开头
       使用RequestMapping修饰的方法可以处理请求的类似Servlet中的doGet doPost

       位置:在方法的上面或者在类的上面

       返回值:ModelAndView:表示本次请求的处理结果
             Model:数据,请求处理完成后,要显示给用户的数据
             View:视图,比如jsp等等
     */
   // @RequestMapping(value = "/some.do")
    @RequestMapping(value = {"/some.do","/first.do"})   //value里面可以写多个名字进行访问
    public ModelAndView doSome(){ //doSome相当于doGet()---调用Service请求处理
        System.out.println("调用Sevice后:");//这里先不调用,简化处理
        ModelAndView mv=new ModelAndView();

        //添加数据,框架在请求的最后把数据放入到request作用域。
        //request.setAttribute("msg","欢迎使用mvc开发")
        mv.addObject("mgs","欢迎使用mvc开发");
        mv.addObject("fun","执行的是dosome方法");

        //指定视图的完整路径
        //框架对视图执行的forword操作,request.getRequestDisPatcher("/show.jsp").forward(..)
       // mv.setViewName("/show.jsp");

        //mv.setViewName("/WEB-INF/view/show.jsp");//名字很长,可以通过视图解析器

        /*
        * 当配置了视图解析器后,可以使用逻辑名称(文件名),指定视图
        * 框架会使用视图解析器的前缀+逻辑名称+后缀组成完整的路径,这里就是字符连接操作
        * /WEB-INF/view/ + show + jsp
        * */
        mv.setViewName("show");

        //返回mv
        return mv;

    }

    //一个控制器中可以定义多个方法
    @RequestMapping(value = {"/other.do","/second.do"})   //value里面可以写多个名字进行访问
    public ModelAndView doOther(){
        System.out.println("调用Sevice后:");//这里先不调用,简化处理
        ModelAndView mv=new ModelAndView();

        //添加数据,框架在请求的最后把数据放入到request作用域。
        //request.setAttribute("msg","欢迎使用mvc开发")
        mv.addObject("mgs","欢迎========使用mvc开发==============");
        mv.addObject("fun","执行的是dosome方法");
        mv.setViewName("other");

        //返回mv
        return mv;

    }
}

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 17:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>


    Title
    


  

hellow word

发起some.do请求

发起some.do请求

show.jsp:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 19:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>


    Title
    


 

/WEB-INF/view/show.jsp,从request作用域中获取数据

msg数据:${mgs}

fun数据:${fun}

other.jsp:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 19:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>


    Title
    


 

/WEB-INF/view/other.jsp,从request作用域中获取数据

msg数据:${mgs}

fun数据:${fun}

点击第一个:

 

从地址栏输入第一个方法访问路径的另一个名字

 

点击第二个:

 

 地址栏输入第二个方法访问的另外一个路径

 

在一个控制类中,通过不同的地址,访问同一个方法

也可以在一个类中创建多个方法,处理不同的请求:

比如做一个用户表 增删改查,做4个方法就行了,一个方法对应着一个操作,就不像跟以前一样了,写Servlet的时候,一般情况下一个Servlet对应一个方法,一个Servlert对应一个请求,写4个请求,需要写4个Servlet,而现在不用了,只要写4个方法就够了,这是方便之处。

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章