欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

struts2中把action中的值传递到jsp页面的例子

发布时间:2025/4/9 编程问答 39 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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>&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保存数据的作用域,时间域,区别和联系?

 

转载于:https://www.cnblogs.com/Berryxiong/p/6115958.html

总结

以上是生活随笔为你收集整理的struts2中把action中的值传递到jsp页面的例子的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。