Output data: how to bring the data page || SpringMVC except through the upload into the native method request in session and also how to bring the data page

Output data: how to bring the data page


In addition to SpringMVC been uploaded into the method native request and session outside but also how to bring the data page

 

 







SpringMVC can provide a temporary field to save the data Session of the way



web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>5.SpringMVC_output</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!-- 字符编码过滤器 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	
</web-app>

springDispatcherServlet-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<context:component-scan base-package="com.atguigu"></context:component-scan>

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

index.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>

<!-- SpringMVC如何给页面携带数据过来; --><br/>
<a href="handle01">handle01</a><br/>
<a href="handle02">handle02</a><br/>
<a href="handle03">handle03</a><br/>
<a href="handle04">handle04</a>

</body>
</html>

success.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>
<h1>Hello</h1>
pageContext:${pageScope.msg }<br/>
request:${requestScope.msg }<br/>
session:${sessionScope.msg }-${sessionScope.haha}<br/>
application:${applicationScope.msg }<br/>
<%System.out.println("来到页面了...."); %>
</body>
</html>

OutputController.java

package com.atguigu.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@SessionAttributes(value={"msg"},types={String.class})
@Controller
public class OutputController {
	
	//args:如何确定目标方法每一个参数的值;最难?
	// method.invoke(this,args)
	@RequestMapping("/handle01")
	public String handle01(Map<String, Object> map){
		map.put("msg", "你好");
		map.put("haha", "哈哈哈");
		System.out.println("map的类型:"+map.getClass());
		return "success";
	}
	
	/**
	 * Model:一个接口
	 * @param model
	 * @return
	 */
	@RequestMapping("/handle02")
	public String handle02(Model model){
		model.addAttribute("msg", "你好坏!");
		model.addAttribute("haha", 18);
		System.out.println("model的类型:"+model.getClass());
		return "success";
	}
	
	@RequestMapping("/handle03")
	public String handle03(ModelMap modelMap){
		modelMap.addAttribute("msg", "你好棒!");
		System.out.println("modelmap的类型:"+modelMap.getClass());
		return "success";
	}
	
	/**
	 * 返回值是ModelAndView;可以为页面携带数据
	 * @return
	 */
	@RequestMapping("/handle04")
	public ModelAndView handle04(){
		//之前的返回值我们就叫视图名;视图名视图解析器是会帮我们最终拼串得到页面的真实地址;
		//ModelAndView mv = new ModelAndView("success");
		ModelAndView mv = new ModelAndView();
		mv.setViewName("success");
		mv.addObject("msg", "你好哦!");
		return mv;
	}
}

Published 434 original articles · won praise 105 · views 70000 +

Guess you like

Origin blog.csdn.net/qq_39368007/article/details/104986251