How to display velocity form values on another velocity template?

Chong Jing Hong June 9, 2021

I am developing a plugin that has a web-item connected to an xwork module, which displays a velocity template with a form, as such:

<div>                
<form id="form" class="aui" method="post" action="#">
<fieldset>                        
<button type="button" onClick="fetchAllPagesTitlesOnClick()" style="margin: 2vh auto; display: block;">Get All Pages</button>
<div style="display: block;"></div>
<label for="pageTiles" style="margin: 2vh 2vw;">Select Page Title:</label>                        
<select name="pageTitles" id="pageTitleSel" style="margin: 2vh auto;" onchange="fetchVersionsOfPage()"></select>
<button type="button" onClick="fetchPageVersionsOnClick()" style="margin: 2vh auto; display: block;">Get All Versions</button>
<label for="pageVersion1" style="margin: 2vh 2vw;">Select Page Version:</label>                        
<select name="pageVersion1" style="margin: 2vh auto;" id="pageVersionSel1" onChange="fetchPageVersionContent(1)"></select>          
<div style="display: block;"></div>
<label for="pageVersion2" style="margin: 2vh 2vw;">Select Page Version:</label>                        
<select name="pageVersion2" style="margin: 2vh auto;" id="pageVersionSel2" onChange="fetchPageVersionContent(2)"></select>
<div style="display: block;"></div>
<button type="submit" form="form" value="Submit" style="margin: 2vh auto; display: block;">Submit</button>
</fieldset>                
</form>            
</div>

 My question is: how do I post the selected values onto another velocity template?

1 answer

1 accepted

0 votes
Answer accepted
Alex Medved _ConfiForms_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 9, 2021

HI

I believe you should define an xwork action (in your plugin) and post to this action (which in turn does what you need and render the "other" template).

https://developer.atlassian.com/server/confluence/adding-a-custom-action-to-confluence/ 

Alex

Chong Jing Hong June 9, 2021

Hi Alex, thanks for replying. I did what you said and created another xwork action, as such:

<xwork name="test-xwork" key="test-xwork">        
<description key="test-xwork.desc">#</description>        
<package name="test" extends="default" namespace="/test">            
<default-interceptor-ref name="validatingStack"/>            
<action name="test-xwork-action" class="com.example.plugin.AnotherAction">                
<result name="success" type="velocity">/velocity/result.vm</result>            
</action>        
</package>    
</xwork>

and add action="/test/test-xwork-action" into the form tag.

But when I submitted the form, Http status 404 came up. What did I do wrong?

Alex Medved _ConfiForms_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 9, 2021

Could be multiple reasons

  • which class does your action extends? 
  • name space issue… meaning- incorrect url (without a context or something like that)
  • maybe something else… hard to tell from the shared snippets 

Alex

Chong Jing Hong June 9, 2021

Thanks for the reply, Alex. The action class extends ConfluenceActionSupport

Alex Medved _ConfiForms_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 9, 2021

The actions are called via .action

/test/test-xwork-action.action

And you probably need to have a context path in front (for example if Confluence is run in /wiki or /confluence web context) 

${req.contextPath}/test/test-xwork-action.action

404 may also be thrown when the template to render cannot be found ( make sure the path is correct - /velocity/result.vm)

Alex

Chong Jing Hong June 10, 2021

Hey Alex, your solution works. Thanks so much! One last question, how do I access the selected values from the first velocity template (the one with the form) from the second velocity template?

Alex Medved _ConfiForms_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 10, 2021

The best way is to define a parameter in your action with the same name as the parameter in the form (select or input) and create getters/setters

this way the parameter will be automatically populated from form (request) to action 

action classes in Confluence/xwork are thread safe (new instance is created for each request)

Chong Jing Hong June 10, 2021

But how will I be able to get the parameter value from the Java action class to the template?

Alex Medved _ConfiForms_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 10, 2021

Basically, the same way... defining the field in your class and having getters to get it's value

Assuming the field name is "myfield" and you have defined a getter: 

public String getMyfield() {
return myfield;
}

In this case you can access it in the Velocity template via ${myfield}

Alex

Chong Jing Hong June 11, 2021

Awesome! Thank you so much Alex!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events