Hi i have two file upload field in my Struts2 jsp i want to have the first field to be mendatory and the second may be empty. How could i do this using fileupload interceptor
Thanks
I will suggest you to do following - Write 2 methods in struts 2 Action class
validate() - this method will be called on upload button click call javascript function and submit Ajax request to Struts 2 Action, and validate() method of this Action will just check weather user has browsed atleast one file and will return true or false value based on this.
execute() - if validation() method returns true then only submit the form (submit form through javascript), so that it will call execute() method and perform file upload logic here.
Related
We have a number of Freemarker pages that have action calls within them, for example:
The problem we are having is that if the action that renders the page returns INPUT for any reason then the execute() method of the included action does not get called. Instead, only the validate method gets called. This means that the logic for the action is not executed. I would have thought that the included actions invocation should be independent of the result of the 'parent' action.
To workaround we are having to add input result mappings to the header action mapping to exactly the same as success mappings, and also explictly call execute within the validate method. This is unclean and seems wrong.
Is this a struts2 bug or is there something I am missing?
Thanks
Matt
I have a jsp page called diary.jsp where the action class associated with it is called DiaryAction and in this class I am using the populate method to prepopulate fields. This is working as expected when I first access the page using PopulateDiary.action in the URL i.e.
<li>Profile</li>
However as part of this page I have a form where a user can add a new diary entry. On the submit of this form I call the following action:
<action name="addDiaryEntry" method="addDiaryEntry"
class="spirit.of.community.action.classes.DiaryAction">
<result name="success">/diary.jsp</result>
</action>
This is calling the correct code to add the new diary entry. However, on the success of this action method I want to display the same page i.e diary.jsp with the populated data again (to include the updated entry). I have placed this line inside my diary.jsp page in the hope that this would accomplish my goal:
<s:action name="populateDiary"></s:action>
Now when the diary.jsp loads I can see the populate method being called. But the page isn't populated with any of my data. If its of any importance, the URL when the form is submitted is - "/addDiaryEntry.action". Can anyone advise how I could call my addEvent method to add a new entry, and then reload the page with the updated data? Any advice is appreciated! Thanks in advance!
You have few ways to accomplish this and here they are
When you call addDiaryEntry action to add a diary instance you can create a list of all entries and can make use of iterator to show the data as per your way.
Other way is to <s:action name="populateDiary" executeResult="true">.
execute result tell whether the result of this action (probably a view) should be executed/rendered.
For more details refer to the Execute action
I have a class A :
class A extends ActionSupport{
int someId;
// getters/setters
public String execute(){
setSomeId(2);
return SUCCESS;
}
public String save(){
// something
}
}
In struts.xml, I have configured an action "ViewId" that takes us to the default method execute, where someId is set. Then, we are taken to a jsp page show.jsp where I can access the someId value. In show.jsp, I have to enter an email id and then submit the page. The action that's now called in "Save" that takes us to the save method of the action class. But, I have given some checks in the corresponding validation.xml file A-Save-validation.xml, which will check the email entered for a format. The problem is that if the xml validation fails, we are taken back to show.jsp, but the viewId parameter is now not available. Why is this so ?
The input page should appear similar to the user as before. Only the fields that are now validated should have an error page associated with them. Any workaround for this ?
Like #Umesh said, validation happens on the corresponding interecptor, before the action's method is called.
When validation fails, the action's method is never called and you will be taken to the INPUT result.
In order to achieve what you want you have some options:
Implement the preparable interface in your action
Peform the validation inside your method.
Use s:action in your jsp before the items you want populated to call an action that populates the relevant section
1 is probably the easiest. I like option 3 as well.
I am struts2 for developing my application.
Sample code of action class would be
class sampleAction extends Action {
private List<Employee> employee;
public validate(){
--logic for validation
}
public String prepopulate(){
--logic for populating value of employee list
}
--getters and setters
}
Now my problem is on page load i call prepopulate function and populate the value of employee list. After page submit validate method is called and during that if some error happens control redirects to jsp. but this time the value of employee list is empty. I am using this list for autocompleter tag in struts2.
I have never used Struts 2 built-in validation mechanism, as I prefer client-side validation to avoid an extra round trip. This is purely a personal choice and not a standard.
First I will suggest you not to use Action and use ActionSupport: ActionSupport provides a lot of functionality out of the box and you need not to do everything yourself.
I am assuming that you are using defaultStack and if this is the case than it provides out of the box Prepare Interceptor which takes care of preparing any values before the action itself is called.
In your case, validate is called before the execute method, so you never will get a chance to re-fill the values you need in your JSP.
All you need to make sure that you have prepare() method in your action class. Here are more details for this interceptor:
Prepare Interceptor
FAQ: How do we repopulate controls when validation fails
I need to create a drop down menu in struts2 from List. How can I create an action class so that when a .jsp page is loaded, it populates the list first?
i found this link http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/ , it works too but for it work user must go to http://localhost:8080/Struts2Example/selectAction.action to make it work but what if someone were to directly go to select.jsp.
Since you're using a .jsp, you could load the dropdown with a scriptlet before you render the <s:select> tag.
However, it's better practice to allow the action to perform the loading and hide the .jsp files under /WEB-INF so they're not directly accessible. A common approach to perform this is the Prepare interceptor.
If you've got it in your interceptor stack, it will automatically invoke any method with the following name in your action before invoking the requested method:
prepare{MethodName}()
prepareDo{MethodName}()
prepare()
That means you can do something like the following in your Action:
public class YourAction extends ActionSupport {
public String prepare(){
// populate your drop down object
}
public String view(){
// forward to your jsp
return SUCCESS;
}
}
Then all you have to do is call your action's view() method and prepare will be called first by Struts.