SpringMVC -- 梗概--源码--壹--跳转

1.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 前端控制器 
         /=默认的url-pattern
         /a/b/c  /a
         
         /a/d/c
         /a/d
         /a
         /
         *注意:此控制器默认加载/WEB-INF下的xxx-servlet.xml文件
                        :其中xxx等于【DispatcherServlet的配置名】
  -->
  <servlet>
      <servlet-name>mvc61</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:mvc62.xml</param-value>
      </init-param>
      <!-- 随项目启动而启动 -->
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>mvc61</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 专治Post请求参数乱码 -->
  <filter>
      <filter-name>encoding61</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <!-- 将请求的编码方式设置为utf-8 -->
      <init-param>
          <param-name>encoding</param-name>
          <param-value>utf-8</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>encoding61</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

2.配置控制器

Class : JumpController

package com.c61.controller;

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.c61.entity.User;
import com.c61.entity.ValueObject;

/**
 * 
 * @author admin
 * 收参测试  controller
 */
@Controller
@RequestMapping(value="/mvc2")//等价于namespace
public class JumpController {
    
    @RequestMapping("/jump1")
    public String testJump1(){
        System.out.println("jump1");
        return "forward:/abc.jsp";//转发到 /abc.jsp
    }
    @RequestMapping("/jump2")
    public String testJump2(){
        System.out.println("jump2");
        return "redirect:/view/abc.jsp";//重定向到 /abc.jsp
    }
    
    /**
     * 如下两个方法中在进行 C --> C 跳转
     * 可以写相对路径或绝对路径
     * 但是,进行跨namespace的跳转时,必须要用绝对路径
     * @return
     */
    @RequestMapping("/jump3")
    public String testJump3(){
        System.out.println("jump3");
        //return "forward:jump1";//转发跳转到jump1中,【jump1】相对路径,相对于当前的namespace
                               //即,【jump1】等价于【/mvc2/jump1】
        return "forward:/mvc2/jump1";//转发跳转到/mvc2/jump1中,绝对路径
    }
    
    @RequestMapping("/jump4")
    public String testJump4(){
        System.out.println("jump4");
        //return "redirect:jump2";//重定向跳转到jump1中,【jump2】相对路径,相对于当前的namespace
                               //即,【jump2】等价于【/mvc2/jump2】
        //return "redirect:/mvc2/jump2";//重定向跳转到/mvc2/jump2中,绝对路径
        return "redirect:/mvc3/jump9";
    }
    
    //了解  ModelAndView
    @RequestMapping("/jump5")
    public ModelAndView testJump5(){
        System.out.println("jump5");
        ModelAndView mav=new ModelAndView();
        mav.addObject("name", "lime");
        mav.setViewName("forward:/mvc3/jump9");
        return mav;
    }
}

Class : JumpController2

package com.c61.controller;

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.c61.entity.User;
import com.c61.entity.ValueObject;

/**
 * 
 * @author admin
 * 收参测试  controller
 */
@Controller
@RequestMapping(value="/mvc3")//等价于namespace
public class JumpController2 {
    
    @RequestMapping("/jump9")
    public String testJump1(){
        System.out.println("jump9 in mvc3");
        return "forward:/abc.jsp";//转发到 /abc.jsp
    }
    
}

3 配置视图

View : jump.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <form action="${pageContext.request.contextPath}/mvc2/jump1">
        <input type="submit" value="提交"/>
    </form>
  </body>
</html>

View : abc.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    This is my abc JSP page. <br>
    name:${requestScope.name}
  </body>
</html>

View : /view/abc.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    This is my abc JSP page. <br>
  </body>
</html>

啦啦啦

posted @ 2017-04-13 18:18  limeOracle  阅读(230)  评论(0编辑  收藏  举报