例子:

RegistAction的代码:

  1. package com.wss.action;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4.  
  5. import org.apache.struts2.ServletActionContext;
  6.  
  7. import com.opensymphony.xwork2.ActionContext;
  8. import com.opensymphony.xwork2.ActionSupport;
  9. import com.wss.Dao.School;
  10. import com.wss.Dao.User;
  11. import com.wss.Dao.UserDao;
  12.  
  13. public class RegistAction extends ActionSupport {
  14.  
  15. public RegistAction()
  16. {
  17. System.out.println("Initialization RegistAction....");
  18. }
  19. //user有Get和Set方法,是类成员,只需要赋值就可以
  20. private User user =new User();
  21. //private User user;
  22.  
  23. public User getUser() {
  24. System.out.println("Getting the getUser");
  25. return user;
  26. }
  27.  
  28. public void setUser(User user) {
  29. System.out.println("Setting the setUser");
  30. this.user = user;
  31. }
  32.  
  33. //school有Get和Set方法,是类成员,只需要赋值就可以
  34. private School school;
  35. public School getSchool() {
  36. System.out.println("Getting the getSchool");
  37. return school;
  38. }
  39.  
  40. public void setSchool(School school) {
  41. System.out.println("Setting the setSchool");
  42. this.school = school;
  43. }

  44. //company有Get和Set方法,是类成员,只需要赋值就可以
  45. private String company;
  46. public void setCompany(String company)
  47. {
  48. System.out.println("Setting the company");
  49. this.company=company;
  50. }
  51.  
  52. public String getCompany()
  53. {
  54. return this.company;
  55. }
  56.  
  57. public String execute() throws Exception{
  58. UserDao ud =new UserDao();
  59. //ActionContext ctx = ActionContext.getContext();
  60. //HttpServletRequest request = (HttpServletRequest) ctx.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
  61. //request.setAttribute("company", this.company);
  62.  
  63. System.out.println("The company is "+this.company+" The name is "+this.user.getName()+" The address is "+this.user.getAddress());
  64. System.out.println("The school name is "+this.school.getName()+" The city is "+this.school.getCity()+" The department is "+ this.school.getDepartment());


  65. ActionContext.getContext().put("message","注册成功");
  66. ServletActionContext.getRequest().setAttribute("school","北京大学");
  67. //if(ud.regist(user)!=0){
  68.  
  69. ActionContext.getContext().getSession().put("welcome", "欢迎访问");
  70. ServletActionContext.getRequest().getSession().setAttribute("city", "北京,上海,深圳");
  71.  
  72. String label="标签内容";
  73. ActionContext.getContext().put("label", label);
  74. this.addFieldError("success", "成功");
  75. return SUCCESS;
  76. //}
  77. //this.addFieldError("error", "注册失败");
  78. //return ERROR;
  79.  
  80. }
  81. }

regist.jsp代码:

  1. <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
  2. <%@ page contentType="text/html;charset=gbk"%>
  3. <%@ taglib prefix="s" uri="/struts-tags" %>
  4.  
  5. <%
  6. String path = request.getContextPath();
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  8. %>
  9.  
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  11. <html>
  12. <head>
  13. <base href="<%=basePath%>">
  14.  
  15. <title>京东商城注册页面</title>
  16. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  17.  
  18. <%
  19. request.setCharacterEncoding("gbk");
  20. %>
  21. </head>
  22.  
  23. <body>
  24. <center>
  25. <form action="regist" method="post">
  26.  
  27. 用户名:<input type="text" name="user.name"/><br>
  28. 密&nbsp;&nbsp;码:<input type="password" name="user.password"/><br>
  29. 手&nbsp;&nbsp;机:<input type="text" name="user.phone" /><br>
  30. 地&nbsp;&nbsp;址:<input type="text" name="user.address"/><br>
  31.  
  32. 公&nbsp;&nbsp;司: <input type="text" name="company"/> <br>
  33.  
  34. 学&nbsp;&nbsp;校:<input type="text" name="school.name"/>
  35. 城&nbsp;&nbsp;市:<input type="text" name="school.city" />
  36. 院&nbsp;&nbsp;系:<input type="text" name="school.department" />
  37.  
  38. <table>
  39. <tr>
  40. <td><input type="submit" value="注册"/></td>
  41. <td><input type="reset" value="重置" ></td>
  42. </tr>
  43. </table>
  44. </form>
  45. <s:fielderror />
  46.  
  47. </center>
  48. </body>
  49. </html>

login.jsp代码:

  1. <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
  2. <%@ taglib prefix="s" uri="/struts-tags" %>
  3.  
  4. <%
  5. String path = request.getContextPath();
  6.  
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  8. %>
  9.  
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  11. <html>
  12. <head>
  13. <base href="<%=basePath%>">
  14.  
  15. <title>京东商城</title>
  16. <style>
  17. .head ul{
  18. width:980px;
  19. border:1px;
  20. solid:#000;
  21. margin:0 auto;
  22. }
  23. .head ul li{
  24. float:left;
  25. }
  26. .head ul li a{
  27. width:80px;/*设置元素宽为80px*/
  28. height:28px;/*设置高度为28px*/
  29. line-height:28px;/*设置行距为28px,让文字在每行的中间位置*/
  30. background:#3A5FCD;/*设置元素的背景为红色*/
  31. color:#FFF;/*文字颜色是白色*/
  32. margin:5px 10px;
  33. font-size:12px;/*用12号字*/
  34. display:block;/*这个比较关键,因为a本身就是联级元素,本身不具有宽高,用这个把它变成块级元素,这样前面设置的宽和高就能起作用了*/
  35. text-align:center;/*让文本居中*/
  36. text-decoration:none; /*去掉下划线*/
  37. }
  38.  
  39. .head ul li a:hover{ /*这个大概的意思就是当鼠标放到这个a元素的上面时,这个a元素的样式就按下面的代码执行*/
  40. width:78px;
  41. height:26px;
  42. line-height:28px;
  43. border:1px solid red;
  44. color:white;
  45. background:#40E0D0;
  46. }
  47. </style>
  48. <script type="text/javascript">
  49. function forword(){
  50. window.location.href="regist.jsp";
  51. }
  52. </script>
  53.  
  54. </head>
  55.  
  56. <body>
  57. <center>
  58. <div class="head">
  59. <ul>
  60. <li><a href="login.jsp">首页</a></li>
  61. <li><a href="">商品</a></li>
  62. <li><a href="">用户信息</a></li>
  63. <li><a href="">购物车</a></li>
  64. <li><a href="">发现</a></li>
  65. <li><a href="">请联系我们</a></li>
  66. </ul>
  67. </div>
  68. <h2><font color="#FF7F00">登录</font></h2>
  69. <form action="login" method="post" name="myform">
  70.  
  71. 用户名:<input type="text" name="name" /><br>
  72. 密&nbsp;&nbsp;码:<input type="password" name="password"/><br>
  73. <table>
  74. <tr>
  75. <td><input type="submit" value="登录"/></td>
  76. <td><input type="reset" value="重置" ></td>
  77. <td><input type="button" value="注册" onClick="forword()"></td>
  78. </tr>
  79.  
  80. </table>
  81. </form>
  82. <s:fielderror>
  83. <s:param>success</s:param>
  84. </s:fielderror>
  85.  
  86. 类属性 company(el表达式): ${company}</br>
  87. 类属性 用户名 (el表达式):${user.name}</br>
  88. 方法值 request 注册 (el表达式):${message }</br>
  89. 方法值 session 欢迎词 (el表达式) :${welcome}</br>
  90. 方法值 session 城市 (el表达式):${city}</br>
  91. struts 类属性 用户名:<s:property value="user.name"/></br>
  92. struts 类属性 公司:<s:property value="company"/></br>
  93. struts session welcome:<s:property value="welcome"/> </br>
  94. struts session 城市:<s:property value="city"/></br>
  95. struts 方法值 request 标签:<s:property value="label"/></br>
  96. struts 类属性 school: <s:property value="school.city"/> </br>
  97. 方法值 request 学校 (el表达式):${school} </br>
  98. 方法值 request 标签:${label}
  99. </center>
  100. </body>
  101. </html>

运行结果:

注意:
(1)发现el表达式不管是通过:ActionContext.getContext().put("message",message);

ServletActionContext.getRequest().setAttribute("messae",message);把类属性数据或者方法数据存储到request中,都能用el表达式获得。

(2)方法中的数据值用:

ActionContext.getContext().getSesstion().put("message",message);

ServletActionContext.getRequest().getSesstion().setAttribute("message",message);这两种方法把数据存储到session中,都可以用el表达式获得,并且不需

要${sessionScope.message }中的sessionScope也可以获取数据。

(3)同时struts标签可以对类属性值(自带有set和get就是默认会放入request中)、没有set和get的类属性值编写代码放入request中、方法中的值用代码放入request中

都可以用struts自带的标签<s:property value="message"/>获取数据。

但是struts标签对放入session中的数据不能显示。

如果打开浏览器重新输入http://localhost:8080/ShopDemo/,那么用session保存的数据仍然存在。

浏览器同一个标签,但是前进和退回,在注册页不输入任何的内容,但是当点击注册时候,用ActionContext,ServletActionContext的方法通过request或者session的方法保存的数据,仍然存在;但是通过struts2自动将action的所有带有get,set(这两个方法必须同时有)的属性放入request域中的数据,没有了,显示空值。

 request 和sesstion保存数据的作用域,时间域,区别和联系?

struts2中把action中的值传递到jsp页面的例子的更多相关文章

  1. struts2中怎么把action中的值传递到jsp页面

    对于如何把struts2的action中的值传到jsp页面中,主要的方法有2种: 使用转发视图利用request域中储存所需的值 使用重定向时存储数据进入session使其在jsp中可以获得 下面,让 ...

  2. struts2中,Action通过什么方式获得用户从页面输入的数据,又是通过什么方式把其自身的数据传给视图的?

    struts2中,Action通过什么方式获得用户从页面输入的数据,又是通过什么方式把其自身的数据传给视图的? 解答: 1)可以直接通过与表单元素相同名称的数据成员(需要存在符合命名规范set和get ...

  3. 05. struts2中为Action属性注入值

    概述 struts2为Action中的属性提供了依赖注入功能 在struts2的配置文件中,我们可以很方便地为Action中的属性注入值.注意:属性必须提供get,set方法. 配置 <acti ...

  4. struts2:在Action中使用Servlet的API,设置、读取各种内置对象的属性

    有两种方式可以实现在Action中使用Servlet的API.一种是使用org.apache.struts2.ServletActionContext类,另一种是使用com.opensymphony. ...

  5. Android笔记(二十) Activity中的跳转和值传递

    我们知道,一个APP是由若干个Activity组成的,那么各个Acitivity中肯定需要进行跳转以及传递数值以保证App的运行,现总结一下多个Activity之间的跳转和值传递. 显式Intent跳 ...

  6. Backbone中父子view之间的值传递

    backbone中,使用最多的莫过于在view中进行操作,如模板的渲染以及事件函数的定义.为了提高代码的可维护性,一般地我们会写多个视图即view,将界面按照功能的不同进行模块化划分,模块与view一 ...

  7. ASP.NET Core中的Action的返回值类型

    在Asp.net Core之前所有的Action返回值都是ActionResult,Json(),File()等方法返回的都是ActionResult的子类.并且Core把MVC跟WebApi合并之后 ...

  8. springMVC:将controller中数据传递到jsp页面

    1> 将方法的返回值该为ModelAndView在返回时,将数据存储在ModelAndView对象中如: newModelAndView("/WEBINF/jsp/showData.j ...

  9. 数据库中的记录通过servlet回显到jsp页面中(连接数据库或者查询參照:对数据进行增删改查)

    我们常常会用到通过图书的名称来查询图书那么这种话我们也就会使用到从数据库中搜索出数据而且载入到自己的Jsp页面中 这种话我们须要将从数据库中获取到的数据放进响应中然后通过%=request.getAt ...

随机推荐

  1. POJ 2464 Brownie Points II --树状数组

    题意: 有点迷.有一些点,Stan先选择某个点,经过这个点画一条竖线,Ollie选择一个经过这条直接的点画一条横线.Stan选这两条直线分成的左下和右上部分的点,Ollie选左上和右下部分的点.Sta ...

  2. UVALive 6264 Conservation --拓扑排序

    题意:一个展览有n个步骤,告诉你每一步在那个场馆举行,总共2个场馆,跨越场馆需要1单位时间,先给你一些约束关系,比如步骤a要在b前执行,问最少的转移时间是多少. 解法:根据这些约束关系可以建立有向边, ...

  3. 2014 Super Training #10 C Shadow --SPFA/随便搞/DFS

    原题: FZU 2169 http://acm.fzu.edu.cn/problem.php?pid=2169 这题貌似有两种解法,DFS和SPFA,但是DFS怎么都RE,SPFA也要用邻接表表示边, ...

  4. UESTC 31 饭卡(Card) --背包问题

    背包问题. 思路:如果m<5,此时也不能消费,所以此时答案为m m>=5: 求出背包容量为m-5,买前n-1样便宜的菜(排个序)的最大价值(即最大消费,即消费完后剩余值最接近5)最后减去最 ...

  5. 关于软件测试人员能力模型的建立(from知乎)

    转自: http://www.zhihu.com/question/20254092 测试思维方面:1.测试基础理论(测试流程.测试的基础知识)2.测试用例设计方法论(黑盒.白盒)3.软件质量体系(建 ...

  6. 【MFC】序列化(Serialize)、反序列化(Deserialize)

    1.首先在头文件里面声明 DECLARE_SERIAL(CSelectionSerial) 2.重写CObject的Serialize函数 virtual void Serialize(CArchiv ...

  7. 不支持一个 STA 线程上针对多个句柄的 WaitAll

    [csharp] view plaincopy using System; using System.Collections.Generic; using System.Windows.Forms; ...

  8. SSH公钥认证+优化

    一 ssh公钥认证流程: sshclinet机器:产生公私钥(公钥相当于一把锁) sshclient:将公钥发给sshserver(抛出锁子) sshclinet去连sshserver不需要密钥   ...

  9. C语言 指针与字符串

    C语言可以在栈区 or 堆区 or 全局区 存放字符串,字符串不单单是存储在全局区的. //字符串与指针 #include<stdio.h> #include<stdlib.h> ...

  10. Nutch搜索引擎(第3期)_ Nutch简单应用

    1.Nutch命令详解 Nutch采用了一种命令的方式进行工作,其命令可以是对局域网方式的单一命令也可以是对整个Web进行爬取的分步命令. 要看Nutch的命令说明,可执行"Nutch&qu ...