欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

数据输出:如何将数据带给页面||SpringMVC除过在方法上传入原生的request和session外还能怎么样把数据带给页面

发布时间:2025/4/16 javascript 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 数据输出:如何将数据带给页面||SpringMVC除过在方法上传入原生的request和session外还能怎么样把数据带给页面 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

数据输出:如何将数据带给页面


SpringMVC除过在方法上传入原生的request和session外还能怎么样把数据带给页面

 

 







SpringMVC提供了一种可以临时给Session域中保存数据的方式



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.xsdhttp://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;} }

总结

以上是生活随笔为你收集整理的数据输出:如何将数据带给页面||SpringMVC除过在方法上传入原生的request和session外还能怎么样把数据带给页面的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。