My code is like this:
public class CustoTypeAction extends ActionSupport{
private ResultDto resultDto;
//setter & getter resultDto
}
Config:
<action name="listCustoType" method="listCustomerType" class="custoType">
<result name="success">listCustoType.jsp</result>
How can I set resultDto value to null from jsp on page load? What struts tag should I use?
As I understood you must implement Preparable interface. It executes before execute() method.
http://struts.apache.org/2.1.6/struts2-core/apidocs/com/opensymphony/xwork2/Preparable.html
If you want to show the object only once, and it must be gone after user reloads the page, you must use session. If file is snown already, you can change the value in session that it must not be shown on next reload.
You can see the example to work with session from here:
http://www.roseindia.net/struts/struts2/struts2-login.shtml
Related
I use Java Server Faces and I have some beans. In my xhtml page, I display some drop down lists with the latest selected value at the top. I would like to know how to reinitialize the values when I quit the page (but not the session, by example, by clicking on another page of my web site) and come back to the first page), so that when I come back, my drop down list has the by default value at the top, not the selected one before I quit the page.
Should I put something in the bean constructor ?
The structure of the bean is
#ManagedBean(name = "myBean", eager = true)
#SessionScoped
public class myBean implements Serializable {
....
public myBean () {
...
}
}
You can use onLoad function
<h:body onload="#{app.initilize()}">
where the code goes
#ManagedBean(name = "app", eager = true)
#SessionScoped
public class MyApp {
public void initilize() {
....
}
Since your bean is #SessionScoped, it will only be instantiated once in the session. So, the constructor will be called once.
Use the value from a #RequestScoped or #ViewScoped bean, or set the value in your bean through the controller.
Do not forget to follow Java naming conventions (all classes names begin with uppercase).
I think the scope which applies more suitable to your bean is #RequestScoped (see http://docs.oracle.com/javaee/6/tutorial/doc/gjbbk.html).
I'm creating a web application with Struts 2. In a JSP file, I created a form like this :
<s:form action="actionclassname!%{methodToCall}" method="post">
...
</s:form>
In the action class, I created two methods which will be called depending on the value of the variable methodToCall. That works well.
My problem is that the two methods of the action class need some validations. So I used annotations to validate the form if the first method is called :
#Validations(....)
public String actionMethod1(){
...
}
#Validations(...)
public String actionMehod2(){
...
}
The first method works well with validations. My problem is on the second method. It seems like when I define validations (also using annotations) for this second method, the validations I defined on first method are executed again before those I defined for the second method.
How to make validations on the second method to run when it's this method which is called by the JSP?
In the action configuration via annotation you should use a parameter validateAnnotatedMethodOnly for validation interceptor, like in this example
#Action(value="actionclassname", results = {
#Result(name="input", location = "/actionclassname.jsp")
},interceptorRefs = #InterceptorRef(value="defaultStack", params = {"validation.validateAnnotatedMethodOnly", "true"}))
Can I able to access method as action in Struts 2 with Codebehind plugin? Since there is no struts.xml file and its works as action!method in the URL.
Just I'm wondering to access a method
How to use Struts tag on class?
"#Action(name="action"), <s:url action="action" method="method" />
Class :
#Action(name = "JSONexample")
#Result(name="success",type=JSONResult.class,value="",params={"root","List"})
public class PartAction extends ActionSupport {
public String JSONexample{
-----
return SUCCESS;
}
public String readxml { }
}
if I access below method on submit button click , will I get the JSON data?
$.getJSON('ajax/sayHi.action', formInput,function(data)
So i can access action in URL as
localhost:7001/Sample/JSONmethod.action
to get the JSON data ?
As far as Codebehind Plugin doesn't support actions on methods you need to use Convention Plugin then use convention annotation #Action on method you want to be your action then you don't need ! explicit mapping.
If you continue to use the Codebehind Plugin, then you have to use Dynamic Action Invocation (DMI) to call the method other than mapped to your action.
Another time had to reread you question, it seems you want to access the action that has not mapping to the method other that execute:
It isn't supported by the codebehind plugin;
You have not mapped it via struts.xml. In this case the answer would be - no, you can't. But you can always use execute method!
If you continue with using of DMI better use s:url tag to construct the URL and use the method attribute.
On PAGE A is a table with some data from the database.
by clicking on a row, the page will be forwarded to PAGE B and in the controller the entity will be injected
#DataModelSelection(value = ENTITY_LIST_NAME)
#Out(value = ENTITY_NAME, scope = ScopeType.CONVERSATION, required = false)
private Entity entity;
this works as it should.
The problem is, that our users seems to use bookmark for PAGE B, so the entity will never be injected because they never visited PAGE A
so they always throw this exception
#In attribute requires non-null value
Is there a global function to catch all #In attribute requires non-null value exceptions and forward the user to PAGE C (startpage)?
(of course i can catch this execption on PAGE B but this happens not only on one page, we want to handle this exception on every page)
we are using: jboss5 + jsf 1.2 + seam 2
UPDATE after the answer of EmirCalabuch:
I also tried the hint from EmirCalabuch with:
<page conversation-required="true" no-conversation-view-id="PageC.xhtml" />
but the problem is, that the conversation is alive at this moment, to this forwarding to pageC never happens...
i also made in the page.xml of this page something like:
<action execute="#{controller.checkIfEntityIsSet()}" />
<navigation>
<rule if-outcome="HOME">
<redirect
view-id="/pages/home.xhtml"
/>
</rule>
</navigation>
and in my Controller.java i have somthing like this:
public String checkIfEntityIsSet(){
if(getEntity() == null){
return "HOME";
}
return "";
}
but this checkIfEntityIsSet() is never called, because the #In attribute requires non-null value is thrown before... so this was not a help at all...
Exception handling rules are specified in pages.xml. You could include a rule to catch the org.jboss.seam.RequiredException that is thrown for that type of error and perform your navigation to page C in it.
This however is not a very clean solution as you would bind that exception to that page and most probably you will have this exception elsewhere and would like to redirect to a different page.
A simpler way of achieving the same result is making the conversation required in PageB.page.xml and specifying the view to redirect to when no conversation is active. The page descriptor has an option that allows you to do just that (on PageB.page.xml):
<page conversation-required="true" no-conversation-view-id="PageC.xhtml" />
This tells Seam that if a user tries to display page B and there is no conversation active (which happens when the user gets there from a bookmark), then redirect the user to PageC.xhtml.
Anyway, it takes very little effort to make the page bookmarkable (if you feel your users will be bookmarking it a lot), using page parameters and actions, for example:
In your list page A, instead of an h:commandLink or h:commandButton for each of the rows that take you to page B, use s:link or s:button:
<h:dataTable var="var" value="#{myList.dataModel}">
...
<s:link value="PageB.xhtml">
<f:param name="id" value="#{var.id}" />
</s:link>
...
</h:dataTable>
This will create a link to Page B for each of the entities in the list, passing its ID (for example, PageB.seam?id=1 in the first row, PageB.seam?id=2 in the second and so on. These links are bookmarkable.
On PageB.page.xml declare the parameter:
<param name="id" value="#{myHomeComponent.id}" />
Where myHomeComponent is a component of type EntityHome<YourEntity>. You can then use #{myHomeComponent.instance} inside Page B to access the entity selected.
i managed it now different:
in the Controller.java i have for the initialization something like this:
#Create
public void initialize() throws MissingMyEntityException {
if(qualifiedCustomer == null){
throw new MissingMyEntityException("something wrong");
}
....
}
my MissingMyEntityException.java looks like this:
public class MissingMyEntityException extends Exception {
private static final long serialVersionUID = 8640645441429393157L;
public MissingMyEntityException(String message) {
super(message);
}
}
and in the pages.xml i have the exception handler like this:
<exception class="com.dw.companyName.view.xyz.exception.MissingMyEntityException">
<redirect view-id="/pages/home.xhtml">
<message>something went wrong</message>
</redirect>
</exception>
and this fixed the problem.
but thanks for your help, but it was not working your way :(
function redirect(id){
alert(id);
document.forms["AddToCart"].submit();
}
This is my javascript. How can i pass the value of 'id' into AddToCart.java. I am using struts2 framework.
You can store the value in a hidden field inside your form and so when the form is submitted the value will be sent to Action.
<form name="AddToCart" ... >
...
<input type="hidden" id="myid"/>
.....
</form>
then
function redirect(id){
document.getElementById('myid').value = id;
document.forms["AddToCart"].submit();
}
There are many ways to do this and one of the easy way is to pass it as a hidden form field
something like
<s:hidden value="" name="my_ID" id="my_ID"/>
and in you javascript you need to set this hidden input field like
function redirect(id){
alert(id);
document.getElementById("my_ID").value=id;
document.forms["AddToCart"].submit();
}
final step is to create a similar property in your action class with its getter and setters and framework will inject the form value in the respected property
public class MyAction extends ActionSupport{
private String my_ID // data type can be as per your requirements
getter and setters
public String execute() throws Exception{
return SUCCESS;
}
}
this is all you need to do and you will able to get the value inside your action class under my_ID property. I am assuming that AddToCart is your Struts2 Action class else you need to pass the value to your class from your called action.
You can not communicate from client(javascript) to server side(java classes) directly.
Because javascript is executed by your browser and java classes are executed by your server.
So you need to use Ajax request to communicate with java classes.
Since you're submitting a form, you can dynamically create a new input in that form containing the id you wish to send and then submit the form.
We have to do two things to send a value to action class in struts2
send the required value with specific parameter name
create variable with the same name mentioned in jsp & create setter,getter methods for that variable.
in action class
public class AddToCart{
private String itemId;
public String getItemId(){
return itemId;
}
public void setItemId(String id){
this.itemId=id;
}
}
this will work.