<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index.jsp</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/data.action?name=饺子">携带数据进行页面跳转</a>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>main.jsp</title>
</head>
<body>
<h2>显示页面跳转时携带的数据......</h2>
<!-- 在经过页面跳转后,在跳转到的页面里,尝试获取之前存放的数据-->
request: ${requestUser}<br>
httpSession: ${sessionUser}<br>
model: ${modelUser}<br>
map: ${mapUser}<br>
modelMap: ${modelMapUser}<br>
<!-- 尝试直接获取请求地址中携带的参数数据-->
param: ${param.name}
</body>
</html>
package com.example.controller;
import com.example.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Controller
public class DataAction {
@RequestMapping("/data")
//这几个参数都是SpringMVC内置的,可以直接声明使用
public String data(HttpServletRequest request, HttpSession httpSession, Model model, Map<Object, Object> map, ModelMap modelMap){
//User实体类含有两个属性:name(String), age(int)。无参构造方法。全属性的有参构造方法,getter,setter,toString方法
User user=new User("荷包蛋", 20);
//将user对象利用各SpringMVC内置对象存放到相应作用域中
request.setAttribute("requestUser", user);
httpSession.setAttribute("sessionUser", user);
model.addAttribute("modelUser", user);
map.put("mapUser", user);
modelMap.addAttribute("modelMapUser", user);
//最后完成页面转发跳转
return "main";
}
}
package com.example.controller;
import com.example.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Controller
public class DataAction {
@RequestMapping("/data")
public String data(HttpServletRequest request, HttpSession httpSession, Model model, Map<Object, Object> map, ModelMap modelMap){
User user=new User("荷包蛋", 20);
request.setAttribute("requestUser", user);
httpSession.setAttribute("sessionUser", user);
model.addAttribute("modelUser", user);
map.put("mapUser", user);
modelMap.addAttribute("modelMapUser", user);
//最后完成页面的重定向跳转
return "redirect:/admin/main.jsp";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>main.jsp</title>
</head>
<body>
<h2>main......page......</h2>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index.jsp</title>
</head>
<body>
<h2>4种转发和重定向的方式</h2>
<hr>
<a href="${pageContext.request.contextPath}/one.action">1.普通转发页面(对请求的默认处理方式)</a><br><br>
<a href="${pageContext.request.contextPath}/two.action">2.action转发页面</a><br><br>
<a href="${pageContext.request.contextPath}/three.action">3.普通重定向页面</a><br><br>
<a href="${pageContext.request.contextPath}/four.action">4.action重定向页面</a>
</body>
</html>
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class JumpAction {
/**
1.
之前在springmvc.xml中配置了视图解析器,这是SpringMVC处理页面跳转的默认方式,属于普通转发跳转
会将页面转发到action方法的返回值和前缀后缀拼接形成的路径所对应资源页面
*/
@RequestMapping("/one")
public String one(){
System.out.println("one action被访问......");
return "main";
}
/** 2.
* 下面是我们注册的视图解析器的父类:UrlBasedViewResolver,中的几个参数
* 通过对底层源代码的解读,可知在action方法的返回值字符串中,如果以"redirect:"或者"forward:"开头则不会执行视图解析器的路径拼接
* 而是会按照redirect或forward完成页面重定向或页面跳转
*
* public static final String REDIRECT_URL_PREFIX="redirect:";
* public static final String FORWARD_URL_PREFIX="forward:";
* private String prefix="";
* private String suffix="";
*
* 注意:不管要使用action的页面转发或者是action的页面重定向,由于action方法是控制器内部的方法
* 所以要想访问action方法必须访问到控制SpringMVC控制器,而要访问控制器,前提是要能被SpringMVC核心处理器处理(也就是底层的servlet)
* 而要想被底层servlet处理,必须满足请求路径的通配条件,这是我们在web.xml文件中配置好的"*.action"
* 所以要在请求的末尾加上".action"以满足请求的通配要求,才有资格被交给SpringMVC的控制器中的方法处理
*/
@RequestMapping("/two")
public String two(){
System.out.println("other action被访问......");
return "forward:/one.action";
}
/**
* 如果是普通重定向,直接重定向到项目资源,不需要控制器中的action方法的处理,请求路径后不用跟".action",直接写重定向的资源路径即可
*
*/
@RequestMapping("/three")
public String three(){
System.out.println("three action被访问......");
return "redirect:/admin/main.jsp";
}
/**
* 要在请求的末尾加上".action"以满足请求的通配要求,才有资格被交给SpringMVC的控制器中的方法执行
*
*/
@RequestMapping("/four")
public String four(){
System.out.println("other action被访问....");
return "redirect:/three.action";
}
}
合静态html(模拟SpringMVC):
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
马克-to-win@马克java社区:1)在上一个项目中,在src/main目录下,添加resources/static/index.html:(参考目录下:BootSpringMVC)注意:在resources根目录下添加test.html,是访问不到的。在src/main目录下,添加resources/templates/result.html,也是访问不到的。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
index1
<a href="/hello.do">test SpringMvc</a>
</body>
</html>
package com.SpringbootMaven;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
篇幅有限更多请见扩展链接:http://www.mark-to-win.com/tutorial/frame_springBoot_SpringBoothtml.html
*请认真填写需求信息,我们会在24小时内与您取得联系。