struts2中把action中的值传递到jsp页面的例子
例子:
RegistAction的代码:
- package com.wss.action;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- import com.wss.Dao.School;
- import com.wss.Dao.User;
- import com.wss.Dao.UserDao;
- public class RegistAction extends ActionSupport {
- public RegistAction()
- {
- System.out.println("Initialization RegistAction....");
- }
- //user有Get和Set方法,是类成员,只需要赋值就可以
- private User user =new User();
- //private User user;
- public User getUser() {
- System.out.println("Getting the getUser");
- return user;
- }
- public void setUser(User user) {
- System.out.println("Setting the setUser");
- this.user = user;
- }
- //school有Get和Set方法,是类成员,只需要赋值就可以
- private School school;
- public School getSchool() {
- System.out.println("Getting the getSchool");
- return school;
- }
- public void setSchool(School school) {
- System.out.println("Setting the setSchool");
- this.school = school;
- }
//company有Get和Set方法,是类成员,只需要赋值就可以- private String company;
- public void setCompany(String company)
- {
- System.out.println("Setting the company");
- this.company=company;
- }
- public String getCompany()
- {
- return this.company;
- }
- public String execute() throws Exception{
- UserDao ud =new UserDao();
- //ActionContext ctx = ActionContext.getContext();
- //HttpServletRequest request = (HttpServletRequest) ctx.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
- //request.setAttribute("company", this.company);
- System.out.println("The company is "+this.company+" The name is "+this.user.getName()+" The address is "+this.user.getAddress());
- System.out.println("The school name is "+this.school.getName()+" The city is "+this.school.getCity()+" The department is "+ this.school.getDepartment());
- ActionContext.getContext().put("message","注册成功");
- ServletActionContext.getRequest().setAttribute("school","北京大学");
- //if(ud.regist(user)!=0){
- ActionContext.getContext().getSession().put("welcome", "欢迎访问");
- ServletActionContext.getRequest().getSession().setAttribute("city", "北京,上海,深圳");
- String label="标签内容";
- ActionContext.getContext().put("label", label);
- this.addFieldError("success", "成功");
- return SUCCESS;
- //}
- //this.addFieldError("error", "注册失败");
- //return ERROR;
- }
- }
regist.jsp代码:
- <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
- <%@ page contentType="text/html;charset=gbk"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%
- 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>京东商城注册页面</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <%
- request.setCharacterEncoding("gbk");
- %>
- </head>
- <body>
- <center>
- <form action="regist" method="post">
- 用户名:<input type="text" name="user.name"/><br>
- 密 码:<input type="password" name="user.password"/><br>
- 手 机:<input type="text" name="user.phone" /><br>
- 地 址:<input type="text" name="user.address"/><br>
- 公 司: <input type="text" name="company"/> <br>
- 学 校:<input type="text" name="school.name"/>
- 城 市:<input type="text" name="school.city" />
- 院 系:<input type="text" name="school.department" />
- <table>
- <tr>
- <td><input type="submit" value="注册"/></td>
- <td><input type="reset" value="重置" ></td>
- </tr>
- </table>
- </form>
- <s:fielderror />
- </center>
- </body>
- </html>
login.jsp代码:
- <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%
- 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>京东商城</title>
- <style>
- .head ul{
- width:980px;
- border:1px;
- solid:#000;
- margin:0 auto;
- }
- .head ul li{
- float:left;
- }
- .head ul li a{
- width:80px;/*设置元素宽为80px*/
- height:28px;/*设置高度为28px*/
- line-height:28px;/*设置行距为28px,让文字在每行的中间位置*/
- background:#3A5FCD;/*设置元素的背景为红色*/
- color:#FFF;/*文字颜色是白色*/
- margin:5px 10px;
- font-size:12px;/*用12号字*/
- display:block;/*这个比较关键,因为a本身就是联级元素,本身不具有宽高,用这个把它变成块级元素,这样前面设置的宽和高就能起作用了*/
- text-align:center;/*让文本居中*/
- text-decoration:none; /*去掉下划线*/
- }
- .head ul li a:hover{ /*这个大概的意思就是当鼠标放到这个a元素的上面时,这个a元素的样式就按下面的代码执行*/
- width:78px;
- height:26px;
- line-height:28px;
- border:1px solid red;
- color:white;
- background:#40E0D0;
- }
- </style>
- <script type="text/javascript">
- function forword(){
- window.location.href="regist.jsp";
- }
- </script>
- </head>
- <body>
- <center>
- <div class="head">
- <ul>
- <li><a href="login.jsp">首页</a></li>
- <li><a href="">商品</a></li>
- <li><a href="">用户信息</a></li>
- <li><a href="">购物车</a></li>
- <li><a href="">发现</a></li>
- <li><a href="">请联系我们</a></li>
- </ul>
- </div>
- <h2><font color="#FF7F00">登录</font></h2>
- <form action="login" method="post" name="myform">
- 用户名:<input type="text" name="name" /><br>
- 密 码:<input type="password" name="password"/><br>
- <table>
- <tr>
- <td><input type="submit" value="登录"/></td>
- <td><input type="reset" value="重置" ></td>
- <td><input type="button" value="注册" onClick="forword()"></td>
- </tr>
- </table>
- </form>
- <s:fielderror>
- <s:param>success</s:param>
- </s:fielderror>
- 类属性 company(el表达式): ${company}</br>
- 类属性 用户名 (el表达式):${user.name}</br>
- 方法值 request 注册 (el表达式):${message }</br>
- 方法值 session 欢迎词 (el表达式) :${welcome}</br>
- 方法值 session 城市 (el表达式):${city}</br>
- struts 类属性 用户名:<s:property value="user.name"/></br>
- struts 类属性 公司:<s:property value="company"/></br>
- struts session welcome:<s:property value="welcome"/> </br>
- struts session 城市:<s:property value="city"/></br>
- struts 方法值 request 标签:<s:property value="label"/></br>
- struts 类属性 school: <s:property value="school.city"/> </br>
- 方法值 request 学校 (el表达式):${school} </br>
- 方法值 request 标签:${label}
- </center>
- </body>
- </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页面的例子的更多相关文章
- struts2中怎么把action中的值传递到jsp页面
对于如何把struts2的action中的值传到jsp页面中,主要的方法有2种: 使用转发视图利用request域中储存所需的值 使用重定向时存储数据进入session使其在jsp中可以获得 下面,让 ...
- struts2中,Action通过什么方式获得用户从页面输入的数据,又是通过什么方式把其自身的数据传给视图的?
struts2中,Action通过什么方式获得用户从页面输入的数据,又是通过什么方式把其自身的数据传给视图的? 解答: 1)可以直接通过与表单元素相同名称的数据成员(需要存在符合命名规范set和get ...
- 05. struts2中为Action属性注入值
概述 struts2为Action中的属性提供了依赖注入功能 在struts2的配置文件中,我们可以很方便地为Action中的属性注入值.注意:属性必须提供get,set方法. 配置 <acti ...
- struts2:在Action中使用Servlet的API,设置、读取各种内置对象的属性
有两种方式可以实现在Action中使用Servlet的API.一种是使用org.apache.struts2.ServletActionContext类,另一种是使用com.opensymphony. ...
- Android笔记(二十) Activity中的跳转和值传递
我们知道,一个APP是由若干个Activity组成的,那么各个Acitivity中肯定需要进行跳转以及传递数值以保证App的运行,现总结一下多个Activity之间的跳转和值传递. 显式Intent跳 ...
- Backbone中父子view之间的值传递
backbone中,使用最多的莫过于在view中进行操作,如模板的渲染以及事件函数的定义.为了提高代码的可维护性,一般地我们会写多个视图即view,将界面按照功能的不同进行模块化划分,模块与view一 ...
- ASP.NET Core中的Action的返回值类型
在Asp.net Core之前所有的Action返回值都是ActionResult,Json(),File()等方法返回的都是ActionResult的子类.并且Core把MVC跟WebApi合并之后 ...
- springMVC:将controller中数据传递到jsp页面
1> 将方法的返回值该为ModelAndView在返回时,将数据存储在ModelAndView对象中如: newModelAndView("/WEBINF/jsp/showData.j ...
- 数据库中的记录通过servlet回显到jsp页面中(连接数据库或者查询參照:对数据进行增删改查)
我们常常会用到通过图书的名称来查询图书那么这种话我们也就会使用到从数据库中搜索出数据而且载入到自己的Jsp页面中 这种话我们须要将从数据库中获取到的数据放进响应中然后通过%=request.getAt ...
随机推荐
- POJ 2464 Brownie Points II --树状数组
题意: 有点迷.有一些点,Stan先选择某个点,经过这个点画一条竖线,Ollie选择一个经过这条直接的点画一条横线.Stan选这两条直线分成的左下和右上部分的点,Ollie选左上和右下部分的点.Sta ...
- UVALive 6264 Conservation --拓扑排序
题意:一个展览有n个步骤,告诉你每一步在那个场馆举行,总共2个场馆,跨越场馆需要1单位时间,先给你一些约束关系,比如步骤a要在b前执行,问最少的转移时间是多少. 解法:根据这些约束关系可以建立有向边, ...
- 2014 Super Training #10 C Shadow --SPFA/随便搞/DFS
原题: FZU 2169 http://acm.fzu.edu.cn/problem.php?pid=2169 这题貌似有两种解法,DFS和SPFA,但是DFS怎么都RE,SPFA也要用邻接表表示边, ...
- UESTC 31 饭卡(Card) --背包问题
背包问题. 思路:如果m<5,此时也不能消费,所以此时答案为m m>=5: 求出背包容量为m-5,买前n-1样便宜的菜(排个序)的最大价值(即最大消费,即消费完后剩余值最接近5)最后减去最 ...
- 关于软件测试人员能力模型的建立(from知乎)
转自: http://www.zhihu.com/question/20254092 测试思维方面:1.测试基础理论(测试流程.测试的基础知识)2.测试用例设计方法论(黑盒.白盒)3.软件质量体系(建 ...
- 【MFC】序列化(Serialize)、反序列化(Deserialize)
1.首先在头文件里面声明 DECLARE_SERIAL(CSelectionSerial) 2.重写CObject的Serialize函数 virtual void Serialize(CArchiv ...
- 不支持一个 STA 线程上针对多个句柄的 WaitAll
[csharp] view plaincopy using System; using System.Collections.Generic; using System.Windows.Forms; ...
- SSH公钥认证+优化
一 ssh公钥认证流程: sshclinet机器:产生公私钥(公钥相当于一把锁) sshclient:将公钥发给sshserver(抛出锁子) sshclinet去连sshserver不需要密钥 ...
- C语言 指针与字符串
C语言可以在栈区 or 堆区 or 全局区 存放字符串,字符串不单单是存储在全局区的. //字符串与指针 #include<stdio.h> #include<stdlib.h> ...
- Nutch搜索引擎(第3期)_ Nutch简单应用
1.Nutch命令详解 Nutch采用了一种命令的方式进行工作,其命令可以是对局域网方式的单一命令也可以是对整个Web进行爬取的分步命令. 要看Nutch的命令说明,可执行"Nutch&qu ...