菜鸟学习Spring——SpringMVC注解版控制层重定向到控制层
来源:互联网 发布:网络语菜鸟是什么意思 编辑:程序博客网 时间:2024/09/21 09:23
转自:http://blog.csdn.net/gwblue/article/details/42968623
执行顺序:Login.jsp-->Login.java-->ListUsername.java-->listUsername.jsp
一、概述。
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、效果图
没有参数的重定向
有参数的重定向
三、总结。
这个功能在我们项目中经常用到一个是没有参数的重定向另一个是有参数的重定向,这样让我们用起来更加方便。
0 0
- 菜鸟学习Spring——SpringMVC注解版控制层重定向到控制层
- 菜鸟学习Spring——SpringMVC注解版控制层重定向到控制层
- 菜鸟学习Spring——SpringMVC注解版控制层重定向到控制层
- Spring的学习--SpringMVC的四个基本注解annotation(控制层,业务层,持久层)
- java 控制层重定向
- Spring 注解学习手札(二) 控制层梳理
- Spring 注解学习手札(二) 控制层梳理
- Spring 注解学习(二) 控制层梳理
- Spring 注解学习手札(二) 控制层梳理
- Spring 注解学习手札(二) 控制层梳理
- Spring 注解学习手札(二) 控制层梳理
- Spring 注解学习手札(二) 控制层梳理
- Spring 注解学习手札(二) 控制层梳理
- springMVC控制层返回视图层
- spring mvc控制层(非注解方式)
- Spring 注解学习手札(二) 控制层梳理GOOD(对照HttpServletRequest 方式)
- SpringMVC的四个基本注解annotation(控制层,业务层,持久层) -- @Component、@Repository @Service、@Controller
- SpringMVC的四个基本注解annotation(控制层,业务层,持久层) -- @Component、@Repository @Service、@Controller
- <学习笔记>apk文件[INSTALL_PARSE_FAILED_NO_CERTIFICATES]错误
- javascript高级开发学习笔记五
- bzoj4291.Kieszonkowe(大根堆 && 贪心)
- JavaScript基础【转载】
- windows环境下的cmd命令行如何用鼠标进行复制粘贴
- 菜鸟学习Spring——SpringMVC注解版控制层重定向到控制层
- MyBatis SQL映射
- asp.net 关于列表问题。点击文章标题,显示文章内容。
- linux 环境 搭建 jenkins
- 69道Spring面试题和答案
- 使用腾讯字蛛生成字体文件
- 验证码倒计时
- python快捷键(不定时更新)
- 浅谈BroadCastReceiver两种注册方式以及其区别