Is it possible to submit a form's data to a java Set in an action of Struts2?
Action code:
class TestAction extends ActionSupport{
private Set<Integer> mySet = new LinkedHashSet<Integer>();
public TestAction(){
}
public String test(){
someMethod(mySet);
}
... Getters/Setters ...
}
Form code:
<form action="test.action" >
<input name="mySet[0]" />
<input name="mySet[1]" />
<input name="mySet[2]" />
<submit />
</form>
The Set is just a collection, and Struts2 has support for any type of collections internally. But for this type of collection you can't use indexes in your OGNL expressions. Try
<form action="test.action" >
<input name="mySet" />
<input name="mySet" />
<input name="mySet" />
<s:submit />
</form>
Related
I am binding a list to field from thymleaf view, but getting null in the controller. Consider it is not null in view.
<form th:object="${obj}"
<input type="hidden" th:field="*{someList}" th:value="${obj.getSomeList()}">
POJO is like this:
public class Foo {
private int id;
private List<Some> someList;
//setter getter
}
If I bind the id in same way I am getting it in controller, pls help If I have take special care for List.
My controller:
#RequestMapping
public String bar(#ModelAttribute("obj") Foo foo)
Hi Anil after setting the values to a variable you need to iterate through the list using the each tag
Pls find the syntax
<form th:object="${obj}" th:action="#{/list}" action="void(0)" method="post">
<tr>
<td th:field="*{id}" th:text="${obj.id}" />
</tr>
<tr th:each="l , i : ${obj.someList}">
<input type="hidden" th:field="*{someList[__${i.index}__].something}" />
</tr>
<input type="submit" class="btn btn-success" />
</form>
In my Jsp page, I am getting the error :
Cannot find any information on property 'productList' in a bean of
type 'Smithd81.InventoryManager'
The InventoryManager class has a getProductList() method which returns a List of Product objects, which I need to access.
In my JSP:
<jsp:useBean id = "productManager" scope = "page" class = "Smithd81.InventoryManager" />
<jsp:getProperty name = "productManager" property = "productList" />
I thought I had this correct on the getProperty property name- starts in lower case and whatnot, which is the typical pitfall for this error, but I definitely have it spelled correctly.
Where I seem to get the error:
<c:forEach var="p" items="${productManager.productList}">
<div>
<form action="inventory" method="POST">
<label>
<span>UPC</span>
<input type="text" name="upc" value="${p.getUpc()}" readonly="readonly"/>
</label>
<label>
<span>Short Details</span>
<input type="text" name="shortDetails" value="${p.getShortDetails()}" />
</label>
<label>
<span>Long Details</span>
<input type="text" name="longDetails" value="${p.getLongDetails()}" />
</label>
<label>
<span>Price</span>
<input type="text" name="price" value="${p.getPrice()}" />
</label>
<label>
<span>Stock</span>
<input type="text" name="stock" value="${p.getStock()}" />
</label>
<input type="submit" name="button" value="Edit" />
<input type="submit" name="button" value="Delete" />
</form>
</div>
</c:forEach>
For clarification, Inside the InventoryManager Class, the method signature reads:
public static List getProductList() throws IOException, ClassNotFoundException {
try {
List<Product> productsList = new ArrayList<>(); //empty product list
Collection<Product> productsFromFile = CollectionFileStorageUtility.load(Product.class);//loads collection from file
productsList.addAll(productsFromFile);// adds all current products from file to the productList List.
return productsList;
} catch (IOException e) {
System.out.println("IOException: error accessing data file.");
return null;
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException: error accessing class.");
return null;
}
}
Refactor your package name to lowercase. Another case is that you are trying to call static method on object (your bean finally is object). So method getProductList() should be non-static.
I have a jsp page and a controller with fillowing functionality:
java controller code:
#Controller
public class AddNewItemController {
#RequestMapping(value = "/newItem/submit", method = RequestMethod.POST)
public String getDataForInterval4(#RequestParam("itemId") String itemId, #RequestParam("product1SkusCnt") String product1SkusCnt, #RequestParam("itemName") String itemName, HttpServletRequest request) {
return "ItemSubmitted";
}
}
my main jsp file that submits to controller:
<center>
<table>
<tr>
<td>How many items do you have?</td>
<td> <input type="number" name="productsCnt" id="productsCnt" size="2" min="1" value="1" onchange="productCount()"/> </td>
</tr>
</table>
</center>
<form action="/newItem/submit" method="post">
<br /><br />
<div id="outerDivContainer">
<div id="product1Div" name="product1Div" >
<hr /> Product 1 Name: <input id="product1Name" /> Product 1 ID: <input id="product1ID" /> How many SKUs of Product 1? <input id="product1SkusCnt" type="number" size="2" min="1" value="1" onchange="skusCount(1)"/> <br /><br />
<div id="skusContainer1">
<div id="sku1Div">
SKU 1 Name: <input id="sku1"/>
</div> <br />
</div>
</div>
</div>
<hr />
<input type="submit" value="Submit" />
</form>
<script>
function productCount() {
console.log("onchange product");
document.getElementById('outerDivContainer').innerHTML = "";
var cnt = document.getElementById('productsCnt').value;
console.log("cnt="+cnt);
for (i=0;i<cnt;i++){
var newEl = document.createElement('div');
newEl.class='prodRow';
j=i+1;
newEl.innerHTML = '<div id="product'+j+'Div"><hr /> Product '+j+' Name: <input id="product'+j+'Name" /> Product '+j+' ID: <input id="product'+j+'ID" /> How many SKUs of Product '+j+'? <input id="product'
+j+'SkusCnt" type="number" size="2" min="1" value="1" onchange="skusCount('+
j+')" /> <br /><br /> <div id="skusContainer'+j+'"><div id="sku1Div"> SKU 1 Name: <input id="sku1"/></div> <br /> </div></div>';
document.getElementById('outerDivContainer').appendChild(newEl);
}
}
function skusCount(productId){
console.log("onchange skus, product id= "+productId+";");
var skusCnt = document.getElementById('product'+productId+'SkusCnt').value;
console.log("skusCnt="+skusCnt);
document.getElementById('skusContainer'+productId).innerHTML = "";
for (i=0;i<skusCnt;i++){
var newEl = document.createElement('div');
newEl.class='skuRow';
j=i+1;
newEl.innerHTML = '<div id="sku'+j+'Div">SKU '+j+' Name: <input id="sku'+j+'" /> </div> <br />';
document.getElementById('skusContainer'+productId).appendChild(newEl);
}
}
</script>
ItemSubmitted.jsp is just a jsp file that confirms successful item submission.
The problem is I don't know how many items will be passed to the controller, or how many skus each item might have.
What would be a suggested approach to this problem? Thanks in advance!
The answer to my question is much simpler than I expected. All I have to do is just know how many parameters I will pass and their names. I don't have to specify it in the controller as #RequestParam("itemId") String itemId, #RequestParam("product1SkusCnt") etc.
Instead I should only pass a request as an argument:
#RequestMapping(value = "/newItem/submit", method = RequestMethod.POST)
public String getDataForInterval4(HttpServletRequest request) { return "ItemSubmitted"}
I can call each of those passed parameters in the controller by doing the following:
request.getParameter("product"+i+"SkusCnt"))
Just need to make sure that I pass the total count of products also.
Hope this helps someone.
I have following situation in code:
Action class:
#NameSpace("/")
public class MyAction extends ActionSupport implements ModelDriven<Car> {
private Car car = new Cart();
#Override
public Car getModel() {
return car;
}
#Action(value = "pageAction", results = {name = SUCCESS, location = "myPage", type="tiles"})
public String showPage() {
return SUCCESS;
}
#Action(value = "formSubmitAction", results = {name = SUCCESS, location = "results.jsp"})
public String formSubmitAction() {
System.out.println(car);
// everything has default values (nulls)
return SUCCESS;
}
}
View for myPage location:
<s:form
namespace="/"
action="pageAction"
method="post" >
<s:push value="model">
<s:textfield name="color" />
<s:textfield name="manufacturer" />
<sj:submit
href="formSubmitAction"
targets="output" />
</s:push>
</s:form>
<div id="output"></div>
results.jsp:
renders empty content into div#output
<s:property value="%{model}" />
<s:property value="%{model.color}" />
<s:property value="%{model.manufacturer}" />
I wonder why is that happening? Model data is not updated after submit.
I'm using struts2-jquery submit tag.
When I'm using simple form submit without Ajax the model is being updated,
but I want to load data asynchronously with Ajax.
How can I achieve that?
The solution is to add ID to form and to sj:submit tag. But I don't know why submit tag inside form wasn't working properly. The correct code is below:
<s:form
id="formId"
namespace="/"
action="pageAction"
method="post" >
<s:push value="model">
<s:textfield name="color" />
<s:textfield name="manufacturer" />
<sj:submit
formIds="formId"
href="formSubmitAction"
targets="output" />
</s:push>
</s:form>
EDIT
As it turns out you only have to add ID to form, and everything works :)
look at link in the comment below
The modelDriven interceptor pushes a model on top of the valueStack. So you can access model properties directly.
<s:property value="%{color}" />
<s:property value="%{manufacturer}" />
Problem
I am using jsp to submit a form and struts 2 action class takes care of it. If there is some problem, then i am sending the result to same page with an error message.
Along with the error message, i want to display property values that he had provided while submitting the request.
Source code
Form contains few text fields and few file type inputs.
My CreateRequest.jsp file:
<input type="file" name="attachment" id="myFile1" />
<input type="file" name="attachment" id="myFile2" />
<input type="file" name="attachment" id="myFile3" />
<input type="text" name="operationName" id="operation1" />
<input type="text" name="operationName" id="operation2" />
<input type="text" name="operationName" id="operation3" />
My Action class :
public class CreateRequest extends ActionSupport {
private List<File> attachment;
private List<String> attachmentContentType;
private List<String> attachmentFileName;
private List<String> operationName
// contains getter and setter for each property
public string execute(){
// some logic
//returns error if it fails otherwise success
}
}
struts.xml (Action Servlet) file:
<action name="createRequest"
class="action.CreateRequest">
<result name="success">RequestStatus.jsp
</result>
<result name="input" >CreateRequest.jsp</result>
<result name="error" >CreateRequest.jsp</result>
</action>
HELP
How do i get all those values displayed in CreateRequest.jsp page, when the action class returns error.
use ognl value=" %{operationName[0]}" for text box