zoukankan      html  css  js  c++  java
  • 菜鸟学习Spring——SpringMVC注解版控制层重定向到控制层

    一、概述。
           SpringMVC中界面请求Contorller1,Contorller1需要重定向到Contorller2中显示其他页面或者做一些业务逻辑,Spring中提供了这个功能利用“redirect:/”来进行重定向。
    二、代码演示。
    1、界面

    Login.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form action="login.spring" method="POST">
    	username:<input type="text" name="username">
    	<br/>
    	<input type="submit" value="登录">
    </form>
    </body>
    </html>


    Login2.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form action="login2.spring" method="POST">
    	username:<input type="text" name="username">
    	<br/>
    	<input type="submit" value="登录">
    </form>
    </body>
    </html>


    listUsername.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
       <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<c:forEach var="eachUsername" items="${listUsername}">
    		${eachUsername }<br/>
    	</c:forEach>
    </body>
    </html>


    2、后台代码

    ListUsername.java

    package com.gaowei.Controller;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @Controller
    public class ListUsername {
    	//无参数
    	@RequestMapping("/listUsername")
    	public String listUsername(Model model){
    		List listUser=new ArrayList();
    		for (int i = 0; i < 10; i++) {
    			listUser.add("username"+(i+1));
    		}
    		model.addAttribute("listUsername",listUser);
    		return "listUsername.jsp";
    	}
    //有参数
    	@RequestMapping("/listUsername2")
    	public String listUsername2(@RequestParam("username") String username,Model model){
    		
    		List listUser=new ArrayList();
    		for (int i = 0; i < 10; i++) {
    			listUser.add(username+(i+1));
    		}
    		model.addAttribute("listUsername",listUser);
    		return "listUsername.jsp";
    	}
    
    }
    


    Login.java

    package com.gaowei.Controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @Controller
    public class Login {
    
    	@RequestMapping("/login")
    	public String login(@RequestParam("username") String username){
    		System.out.print("username="+username);
    		return "redirect:/listUsername.spring";
    	}
    	
    	@RequestMapping("/login2")
    	public String login2(@RequestParam("username") String username){
    		System.out.print("username="+username);
    		return "redirect:/listUsername2.spring?username="+username;
    	}
    }
    


    3、效果图

    没有参数的重定向



    有参数的重定向



    三、总结。

    这个功能在我们项目中经常用到一个是没有参数的重定向另一个是有参数的重定向,这样让我们用起来更加方便。

  • 相关阅读:
    Java自学
    java自学
    每日总结
    每日总结
    每日总结
    每周总结
    每日总结
    每日总结
    每日总结
    每日总结
  • 原文地址:https://www.cnblogs.com/iplus/p/4490356.html
Copyright © 2011-2022 走看看