提交多个值时,execute() 方法不运行

发布于 2024-12-20 15:38:30 字数 634 浏览 2 评论 0原文

我需要提交一些值作为请求参数(通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箜明 2024-12-27 15:38:30

我不确定你想使用 javascript 做什么。但是,这是我们可以做的,让 struts2

在你的操作类中

   private String code
   private String values
// create there getter and setter outside of execute method

为你工作,现在当你提交你的值时创建 2 个属性,例如在你的情况下

 localhost:8080/Com/test.action?code=1001&values=FUEL:PETROL, POWER:KW, Wheel 
Configuration:4X2, CABIN:SINGLE CAB, TRANSMISSION:FULLY AUTOMATIC, Steering: LHD

struts2 在拦截器堆栈中构建将尝试在您的操作类中查找名称为 code、values 的属性,并且由于您已经为这些属性指定了 setter 方法,struts2 将尝试设置这些属性中的值。

在尝试设置属性时,它将查找您为属性指定的数据类型,内置系统将查找它具有的开箱即用类型转换器,并尝试根据您的要求转换数据类型。
如果它有该类型转换器,它将为您工作,否则将抛出一个异常,表明它无法将给定值转换为指定值。

仅供参考:

Struts2 提供了一种将代码与底层 Servlet API 分离的干净方法,因此无需使用 request.getParameter() 或 ActionContext.getContext() 因为框架将为您完成所有这些工作,并以干净的方式为您提供操作类中的所有内容,

这是为我工作的代码

JSP

<script type="text/javascript">
function test(){

    var code='001';

    var values='values=FUEL:PETROL, POWER:KW, Wheel Configuration:4X2, CABIN:SINGLE CAB, TRANSMISSION:FULLY AUTOMATIC, Steering: LHD';
     var value='demo.action?code='+code+'&values='+values;;
     alert(value);
    document.demo.action=value;
    document.demo.submit();

}
</script>
</head>
<body>
<form action="" id="demo" name="demo" method="post">
<input type="button" onclick="test()">

</form>

,这里是 Action 类,

private String code;
private String values;

public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getValues() {
        return values;
    }

    public void setValues(String values) {
        this.values = values;
    }
 public String execute(){
      System.out.println("**************************"+code);
      System.out.println(values);
 }

这里是控制台输出

**************************001
FUEL:PETROL, POWER:KW, Wheel Configuration:4X2, CABIN:SINGLE CAB, 
TRANSMISSION:FULLY AUTOMATIC, Steering: LHD

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

   private String code
   private String values
// create there getter and setter outside of execute method

now when you submit your values for e.g in your case

 localhost:8080/Com/test.action?code=1001&values=FUEL:PETROL, POWER:KW, Wheel 
Configuration:4X2, CABIN:SINGLE CAB, TRANSMISSION:FULLY AUTOMATIC, Steering: LHD

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 use request.getParameter() or ActionContext.getContext() as framework will doing all this work for you and providing you everything inside your action class in clean way

here is the code working for me

JSP

<script type="text/javascript">
function test(){

    var code='001';

    var values='values=FUEL:PETROL, POWER:KW, Wheel Configuration:4X2, CABIN:SINGLE CAB, TRANSMISSION:FULLY AUTOMATIC, Steering: LHD';
     var value='demo.action?code='+code+'&values='+values;;
     alert(value);
    document.demo.action=value;
    document.demo.submit();

}
</script>
</head>
<body>
<form action="" id="demo" name="demo" method="post">
<input type="button" onclick="test()">

</form>

and here is Action class

private String code;
private String values;

public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getValues() {
        return values;
    }

    public void setValues(String values) {
        this.values = values;
    }
 public String execute(){
      System.out.println("**************************"+code);
      System.out.println(values);
 }

here is the console output

**************************001
FUEL:PETROL, POWER:KW, Wheel Configuration:4X2, CABIN:SINGLE CAB, 
TRANSMISSION:FULLY AUTOMATIC, Steering: LHD
素手挽清风 2024-12-27 15:38:30

可能我不太明白你的问题,但我猜你正在寻找这个

<s:url id="urlid" action="anyaction" escapeAmp="false">
       <s:param name="param1">value1</s:param>
       <s:param name="param2">value2</s:param>
 </s:url>

如果你没有设置 escapeAmp="false" 那么你只能发送 1 个变量。

May be I don't understand your question well, but I guess you are looking for this

<s:url id="urlid" action="anyaction" escapeAmp="false">
       <s:param name="param1">value1</s:param>
       <s:param name="param2">value2</s:param>
 </s:url>

If you don't set escapeAmp="false" then you can send only 1 variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文