栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC:第一个程序(动力)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC:第一个程序(动力)

首先配置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
public class MyController {
    
    @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
public class MyController {
    
   // @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");//名字很长,可以通过视图解析器

        
        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个方法就够了,这是方便之处。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985847.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号