當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
数据输出:如何将数据带给页面||SpringMVC除过在方法上传入原生的request和session外还能怎么样把数据带给页面
生活随笔
收集整理的這篇文章主要介紹了
数据输出:如何将数据带给页面||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外还能怎么样把数据带给页面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 请求处理传入原生的API || 请求处理
- 下一篇: 测试ModelAttribute注解