例子:

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>
密&nbsp;&nbsp;码:<input type="password" name="user.password"/><br>
手&nbsp;&nbsp;机:<input type="text" name="user.phone" /><br>
地&nbsp;&nbsp;址:<input type="text" name="user.address"/><br> 公&nbsp;&nbsp;司: <input type="text" name="company"/> <br> 学&nbsp;&nbsp;校:<input type="text" name="school.name"/>
城&nbsp;&nbsp;市:<input type="text" name="school.city" />
院&nbsp;&nbsp;系:<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>
密&nbsp;&nbsp;码:<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页面的例子的更多相关文章

  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. bfs简单题-poj2251

    宽搜基础题 思路很简单,注意细节. 走过的节点一定要打上标记//tag数组 三维字符串输入一定要注意 #include <stdio.h> #include <iostream> ...

  2. python如何控制数据库?

    http://www.w3cschool.cc/python/python-mysql.html 通过利用MySQLdb可以操作数据库 实例: 以下实例链接Mysql的TESTDB数据库: # enc ...

  3. maven - pom.xml 聚合(父)工程 基本内容演示

    企业开发中所用到的基本jar包以及插件都已在此 可以自己根据实际情况酌情增减 <project xmlns="http://maven.apache.org/POM/4.0.0&quo ...

  4. jira与readmine区别

    JIRA适合多人的团队(100+),而Readmine适合中小型团队. Redmine是用ruby开发的基于web的项目管理软件,免费.JIRA收费Redmine可以创建子任务,而jira不易创建子任 ...

  5. MySchool

    USE [MySchool] GO /****** Object: Table [dbo].[Grade] Script Date: 08/06/2014 15:03:17 ******/ SET A ...

  6. StackBlur.js

    StackBlur.js 是 Mario Klingemann 创建的一个快速的.接近高斯模糊的效果库. 更多信息: http://incubator.quasimondo.com/processin ...

  7. 16Mybatis_动态sql_if判断

    mybatis的核心就是动态sql. 什么是动态sql:对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 这篇文章讲解sql中的if语句.它可以对查询条件进行判断,如果输入参 ...

  8. 【WPF】WPF通过RelativeSource绑定父控件的属性

    1.后台代码实现绑定父控件的属性 RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor); //设定为离自己控件 ...

  9. Socket Programming in C#--Introduction

    This is the second part of the previous article about the socket programming. In the earlier article ...

  10. 基于.Net FrameWork的 RestFul Service

    关于本文 这篇文章的目的就是向大家阐述如何在.net framework 4.0中创建RestFul Service并且使用它. 什么是web Services,什么是WCF 首先讲到的是web Se ...