struts2 action中jsp向action传文本框的value,文本框的名字是通过循环动态生成的,该怎么传值呢

action向jsp传值_百度知道
action向jsp传值
action给jsp传值的方式:方法一:使用OGNL表达式。使用struts自带的标签,支持OGNL,比如s:property。&!--test.jsp--&&%@ page contentType=&text/charset=utf-8& pageEncoding=&utf-8&%&&%@ taglib prefix=&s& uri=&/struts-tags& %&&s:property value=&value.name&/&访问LoginAction的时候s:property标签显示getValue().getName();方法二:使用JSP本身的性质。通过request和session来获取值。把Action类改一下:public class LoginAction{public string execute(){SomeBean value=new SomeBean();value.setName(&sfsfjsfje&);ActionContext context=ActionContext.getContext();//往request里放attributecontext.put(&value&,value);//往session里放context.getSession().put(&value&,value);return SUCCESS;}}接下来改页面:&!-- test.jsp --&&%@ page contentType=&text/charset=utf-8& pageEncoding=&utf-8&%&&%@ taglib prefix=&s& uri=&/struts-tags& %&&%= ((SomeBean) request.getAttribute(&value&)).getName() %&&%= ((SomeBean) session.get(&value&)).getName() %&
其他类似问题
为您推荐:
提问者采纳
前台;mes&quot: ${requestjava后台 request.setAttribute(&quot,mes)
提问者评价
you're right!
页面 ${xxx}。;;xxx&quot.setAttribute(&quot。;);r。其他的自己探索;fuck you&quot.getRequest(); 可以这样 方法有多种, &quotHttpServletRequest r = ServletActionContext
其他3条回答
你生成get,方法执行完后return,跳转到的页面上就可以获取到了、set方法后,页面上到request里取、set,可以将mes直接放入request中,在action中对应方法执行时给mes赋值即可,action中的mes变量继不继承的问题先不管你;如果是方法内部的临时变量不用生成get
给mes提供get和set方法,在jsp中 {mes}就可以了
是个${mes}还是{mes} 忘了,都试试吧
有三种方式,第一种:request.setAttribute(&参数&,值);第二种,用session,基本同第一种,session.setAttribute(&参数&,值);第三种:ajax json格式传值,前台解析即可
jsp的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁struts2做jsp页面上拉框的时候传值到action,如何传 - J2EE当前位置:& &&&struts2做jsp页面上拉框的时候传值到action,如何传struts2做jsp页面上拉框的时候传值到action,如何传&&网友分享于:&&浏览:48次struts2做jsp页面下拉框的时候传值到action,怎么传&form action=&search& method=&post& name=&form1&& & &table border=&1& & & &tr&
&td colspan=&3& align=&right&&&select name=&month& id=&month&&
&option&选择月份&/option&
&option value=&1&&s:if test=&optionValue==1&&selected=&selected&&/s:if&&一月&/option&
&option value=&2&&s:if test=&optionValue==2&&selected=&selected&&/s:if&&二月&/option&
&/select&&/td&
&td colspan=&2&&&select name=&huozhong& id=&&&
&option&选择货种&/option&
&option value=&1&&s:if test=&optionValue1==1&&selected=&selected&&/s:if&&人民币&/option&
&option value=&2&&s:if test=&optionValue1==2&&selected=&selected&&/s:if&&美元&/option&
&/td&这是我的jsp页面代码然后action里面应该怎么写,实体类应该怎么写,求详细代码让我研究下,点击查询后下拉框的值要保留。------解决方案--------------------
因为你全放在 form表单中
且是post提交
所以 这样就可以
month,huozhong 在action里可以直接获取到,记得配置struts配置文件
//定义 你jsp中的变量
public String getMonth() {
public void setMonth(String month) {
this.month=
------解决方案--------------------汗 随便找个例子就有吧
------解决方案--------------------action定义变量和下拉款的name一样就可以得到 当前选中的值
------解决方案--------------------用JS 写到hidden 直接一起和FORM提交好了
------解决方案--------------------探讨我的所有代码都写好啦,但是就是没有进入到action里面去, &form action=&search& method=&post& name=&form1&& struts.xml的配置&action name=&search& class=&com.cx.action.Searchaction& method=&dosearch&&&result name=&success&&/awhz……
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有Struts2学习记录之如何在action和jsp页面中传值
接着上一篇,这篇学习如何在action和页面中传值.
一.使用get和set方法
1.首先我们要在action中设置私有属性,并实现其get和set方法
public class UserAction{
public String getUsername() {
public void setUsername(String username) {
this.username =
public String getPassword() {
public void setPassword(String password) {
this.password =
public int getAge() {
public void setAge(int age) {
this.age =
2.在里面增加一个list方法,此方法会跳转到list.jsp页面,并且这个方法中调用set方法就可以传值了
public String list(){
this.setUsername(&小明&);
this.setPassword(&1234&);
this.setAge(15);
return &list&;
3.jsp页面获取,可以用EL表达式,也可以用struts的标签
${username}-----${password}----${age}
二.使用ActionContext来传值
使用这种方式的话,不需要get和set方法,可以在Action中按如下设置参数
public String list(){
ActionContext.getContext().put(&username&,&小明&);
ActionContext.getContext().put(&password&,&1234&);
ActionContext.getContext().put(&age&,12);
return &list&;
然后jsp页面获取
这里注意下,如果使用ActionContext,那么用s:property获取参数的时候,非String类型的需要加#号,不然会报错,类型无法转换过去,开发中为了统一,只要是ActionContext传的参数都会加#号
${username}-----${password}----${age}
三.使用传统方式
传统方式就是request来传值,我们可以通过ServletActionContext来获取这些类的实例
public String list(){
ServletActionContext.getRequest().setAttribute(&username&,&小明&);
ServletActionContext.getRequest().setAttribute(&password&,&12345&);
ServletActionContext.getRequest().setAttribute(&age&,13);
return &list&;
然后jsp访问需要按照以下规则
${username}-----${password}----${age}
四.为什么传值方式是这样的?
struts2采用的ognl这种表达式语言通过简单一致的语法,可以任意存取对象的属性或者调用对象的方法,能够遍历整个对象的结构图,实现对象属性类型的转换等功能。
下面来测试ognl
1.ognl寻找方式
大体上分两种,一种是在root根节点下面寻找,一种是在map集合中寻找
在root中寻找
public void test01() {
User u = new User(1,&tangsheng&,&唐僧&);
Department dep = new Department(&财务处&);
u.setDep(dep);
//第二个参数是root,从根部寻找是不需要加#号的
System.out.println(Ognl.getValue(&nickname&,u));
//对于对象中的其他对象用这种方式索引
System.out.println(Ognl.getValue(&dep.name&,u));
} catch (OgnlException e) {
e.printStackTrace();
在map集合中寻找
public void test02() {
Map ctx = new HashMap();
User u = new User(1,&tangsheng&,&唐僧&);
Department dep = new Department(&财务处&);
u.setDep(dep);
Role r = new Role(1,&超级管理员&);
ctx.put(&user&,u);
ctx.put(&role&, r);
//以下表达式是在root中找,u是root
System.out.println(Ognl.getValue(&username&,ctx,u));
//#user.username是在ctx这个map中找
System.out.println(Ognl.getValue(&#user.username&,ctx,u));
System.out.println(Ognl.getValue(&#role.name&,ctx,u));
//目前root是ctx所以可以直接取得到
System.out.println(Ognl.getValue(&role.name&,ctx,ctx));
//Ognl其实就是一个大的Context,根的key就是root,所以可以通过#root.xx来取值
System.out.println(Ognl.getValue(&#root.username&,ctx,u));
} catch (OgnlException e) {
e.printStackTrace();
对于链表数据的查找
根节点相当于链表的引用就和使用链表本身一样
public void test03() {
List users = new ArrayList();
users.add(new User(1,&ts&,&唐僧&));
users.add(new User(2,&ss&,&沙僧&));
users.add(new User(3,&bj&,&八戒&));
users.add(new User(4,&wk&,&悟空&));
//如果要取list中的元素,需要通过#root[index]来完成取值
System.out.println(Ognl.getValue(&#root[1].nickname&, users));
} catch (OgnlException e) {
e.printStackTrace();
通过ognl调用方法
public void test04() {
List users = new ArrayList();
users.add(new User(1,&ts&,&唐僧&));
users.add(new User(2,&ss&,&沙僧&));
users.add(new User(3,&bj&,&八戒&));
users.add(new User(4,&wk&,&悟空&));
//如果要取list中的元素,需要通过#root[index]来完成取值
System.out.println(Ognl.getValue(&#root[1].nickname&, users));
//Ognl还可以完成方法的调用
System.out.println(Ognl.getValue(&#root[0].sum(1,3)&, users));
= new User();
System.out.println(Ognl.getValue(&hello('world')&, u));
//可以通过调用list中的get()方法获取某个下标的对象,然后完成导航
System.out.println(Ognl.getValue(&get(0).username&, users));
} catch (OgnlException e) {
e.printStackTrace();
2.struts2中传值结构
ValuteStack栈中有两个内容来存储值,一个是CompoundRoot,这个是栈结构,也就是先进后出.而ActionContext是一个map集合,存入键值对,还有request,session等这些值.
对于root根节点,是CompoundRoot这个栈的栈顶元素,对于其他元素则需要使用#root[index]来访问.
对于ActionContext的基本值可以直接#号访问,但对于request这样的对象,则需要#request.name这样的方式来访问.
//可以通过这种方式来操纵栈
ActionContext.getContext().getValueStack().push(user);
效果图如下:
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'struts2 jsp和action相互传值通信的方式
首先是jsp向action传值:首先你的jsp前端代码的name属相在action中必须要有set和get方法,要是属性是引用值得话,在引用类的代码中也要有相应的set和get方法。如:jsp:action:
private IuserPort u = new Iuser();
public String execute() {if (u.find_by_name_pass(user)) {//验证用户
setSession((Map) ActionContext.getContext().getSession());//session作用域
getSession().put("user", user);
return SUCCESS;}this.addActionMessage("请检查您的用户名和密码");return INPUT;
* @return the user
public Iuser getUser() {
* @param user the user to set
public void setUser(Iuser user) {this.user =
* @return the u
public IuserPort getU() {
* @param u the u to set
public void setU(IuserPort u) {this.u =
* @return the session
public Map getSession() {
* @param session the session to set
public void setSession(Map session) {this.session =
}}user的bean:package com.fyz. /** * Iuser entity. @author MyEclipse Persistence Tools */ public class Iuser implements java.io.Serializable { // Fields private Iprivate Sprivate Sprivate S // Constructors /** default constructor */public Iuser() {} public Iuser(String name, String pass){this.name =this.pass =} /** full constructor */public Iuser(String name, String pass, String stat) {this.name =
this.pass =this.stat =} // Property accessors public Integer getId() {return this.} public void setId(Integer id) {this.id =} public String getName() {return this.} public void setName(String name) {this.name =} public String getPass() {return this.} public void setPass(String pass) {this.pass =} public String getStat() {return this.} public void setStat(String stat) {this.stat =} }action向jsp传值: action:
private IuserPort u = new IuserDAO();
public String execute() {if (u.find_by_name_pass(user)) {//验证用户
setSession((Map) ActionContext.getContext().getSession());//session作用域
getSession().put("user", user);
return SUCCESS;}this.addActionMessage("请检查您的用户名和密码");return INPUT;
* @return the user
public Iuser getUser() {
* @param user the user to set
public void setUser(Iuser user) {this.user =
* @return the u
public IuserPort getU() {
* @param u the u to set
public void setU(IuserPort u) {this.u =
* @return the session
public Map getSession() {
* @param session the session to set
public void setSession(Map session) {this.session =
}} jsp: 姓名:
------分隔线----------------------------
------分隔线----------------------------
你可能对下面内容感兴趣:J2EE &&&&最新内容
J2EE &&&&随机内容

我要回帖

更多关于 struts action 的文章

 

随机推荐