【Spring MVC】数据的传递
本文最后更新于 1739 天前,其中的信息可能已经有所发展或是发生改变。

示例controller类:
[sourcecode language=”java” title=”com.controller.DataController.java”]
import javax.servlet.http.HttpSession;

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.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller("DataController")
@RequestMapping("/data")
public class DataController {
@RequestMapping(value={"demo01"}, method={RequestMethod.POST, RequestMethod.GET})
public String demo01(HttpServletRequest request, HttpSession session){
//直接使用API
System.out.println("demo01 is ran…");
request.setAttribute("name", "程州");
session.setAttribute("age", 18);
return "index";
}

//使用Spring MVC框架的Model接口

@RequestMapping("demo02")
public String demo02(Model model){
System.out.println("demo02 is ran…");
model.addAttribute("name", "张三");
return "index";
}

@RequestMapping("demo03")
public String demo03(ModelMap map){
System.out.println("demo03 is ran…");
map.put("name", "李四");
return "index";
}

@RequestMapping("demo04")
public ModelAndView demo04(){
System.out.println("demo04 is ran…");
ModelAndView mav = new ModelAndView();
mav.addObject("name", "老王");
mav.setViewName("index");
return mav;
}
}

[/sourcecode]
展示jsp:
[sourcecode language=”html” title=”index.jsp”]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP ‘index.jsp’ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!–
<link rel="stylesheet" type="text/css" href="styles.css">
–>
</head>

<body>
This is my JSP page. <br>
年龄:${sessionScope.age }<br>
requestName:${requestScope.name }<br>
sessionName:${sessionScope.name }<br>
appName:${applicationScope.name }<br>
</body>
</html>
[/sourcecode]

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇