How to display velocity form values on another velocity template?
Asked by Chong Jing Hong:

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?


Answers:
author: Alex Medved _ConfiForms_
body: 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

comments: author: Chong Jing Hong
body: 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?


author: Alex Medved _ConfiForms_
body: 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


author: Chong Jing Hong
body: Thanks for the reply, Alex. The action class extends ConfluenceActionSupport


author: Alex Medved _ConfiForms_
body: 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


author: Chong Jing Hong
body: 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?


author: Alex Medved _ConfiForms_
body: 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)


author: Chong Jing Hong
body: But how will I be able to get the parameter value from the Java action class to the template?


author: Alex Medved _ConfiForms_
body: 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


author: Chong Jing Hong
body: Awesome! Thank you so much Alex!


Tags: confluence-development

confluence-server

server

velocity