提交多个值时,execute() 方法不运行
我需要提交一些值作为请求参数(通过 javascript),我需要在我的 Action
类中使用它们。我能够以这种方式发布一些数据并在 Action
类中检索。但现在似乎不起作用了。
当我刚刚提交 code=001
时,
document.forms[0].action='test.action?code='+code;
我可以使用 在
Action
类的 execute()
方法中检索该值request.getParameter("code");
但是,当我尝试提交两个值时:
var code='001';
var values='Title:The Boy, Type:Mandatory';
document.forms[0].action='test.action?code='+code+'&values='+values;
在这种情况下,调用甚至不会进入执行方法(我在执行方法中有一个 sysout)。
有人可以告诉我这里出了什么问题吗?我不明白......
谢谢
I need to submit some values as request params (through javascript) which I need to use in my Action
class. I was able to post some data before this way and retrieve in the Action
class. But now it doesn't seem to work.
When I just submit the code=001
,
document.forms[0].action='test.action?code='+code;
I am able to retrieve this value in the execute()
method of Action
class using request.getParameter("code");
However, when I try to submit two values:
var code='001';
var values='Title:The Boy, Type:Mandatory';
document.forms[0].action='test.action?code='+code+'&values='+values;
In this case, the call does not even come into the execute method (I have a sysout in the execute method).
Can someone please tell me what is wrong here? I don't understand.....
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定你想使用 javascript 做什么。但是,这是我们可以做的,让 struts2
在你的操作类中
为你工作,现在当你提交你的值时创建 2 个属性,例如在你的情况下
struts2 在拦截器堆栈中构建将尝试在您的操作类中查找名称为 code、values 的属性,并且由于您已经为这些属性指定了 setter 方法,struts2 将尝试设置这些属性中的值。
在尝试设置属性时,它将查找您为属性指定的数据类型,内置系统将查找它具有的开箱即用类型转换器,并尝试根据您的要求转换数据类型。
如果它有该类型转换器,它将为您工作,否则将抛出一个异常,表明它无法将给定值转换为指定值。
仅供参考:
Struts2 提供了一种将代码与底层 Servlet API 分离的干净方法,因此无需使用 request.getParameter() 或 ActionContext.getContext() 因为框架将为您完成所有这些工作,并以干净的方式为您提供操作类中的所有内容,
这是为我工作的代码
JSP
,这里是 Action 类,
这里是控制台输出
I am not sure what you are trying to do with using javascript.But here is what we can do to let struts2 work for you
in your action class create 2 properties
now when you submit your values for e.g in your case
struts2 build in interceptor stack will try to find property with the name code,values in your action class and since you have already specified setter methods for these properties ,struts2 will try to set the values in these properties.
while trying to set the properties it will look in to the data type you have specified for your properties, build in system will look for out of the box type convertors it has and will try to convert the data type as per your requirements.
if it has that type convertor it will do work for you else will throw an exception that it is unable to convert the given value to the specified value.
FYI:
Struts2 provides a clean way to separate your code from underlying
Servlet API
so there is no need to userequest.getParameter()
orActionContext.getContext()
as framework will doing all this work for you and providing you everything inside your action class in clean wayhere is the code working for me
JSP
and here is Action class
here is the console output
可能我不太明白你的问题,但我猜你正在寻找这个
如果你没有设置 escapeAmp="false" 那么你只能发送 1 个变量。
May be I don't understand your question well, but I guess you are looking for this
If you don't set escapeAmp="false" then you can send only 1 variable.