im in learning struts 2.
i create a simple project that can add and list the PRODUCT that user add. list is my first page and show all product that added. it is my struts.xml file:
<struts>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="listProduct" />
<action name="listProduct" class="control.ProductHandler" method="list">
<result name="success">/list.jsp</result>
</action>
<action name="addProduct" class="control.ProductHandler" method="add">
<result name="success">/listProduct</result>
</action>
<action name="*Form">
<result>{1}.jsp</result>
</action>
</package>
</struts>
when i enter localhost:8080/product in then browser it show me list.jsp file. in this file i have a link that with it i can goto add.jsp file. href of this link is addForm.
in add.jsp file i have 3 text field and a submit button. when entered data added action class return "success" and i most goto localhost:8080/product (default page) but it show me :
HTTP Status 404 - /product/listProduct
this is my action file:
public String list(){
products=Database.get();
return "success";
}
public String add(){
if (add!=null){
Product product=new Product(name, producer, price);
Database.add(product);
}
return "success";
}
where is my mistake?
thanks.
In order to redirect to another action use redirectAction result type.
<action name="addProduct" class="control.ProductHandler" method="add">
<result name="success" type="redirectAction">listProduct</result>
</action>
Related
I have an application where I want to modify(or overwrite) action mapping in struts.xml with new entry.
For detailed explanation here is the scenario.
In my application I have different stages like registration, change_data, I am managing these using context Param.
Web.xml
<context-param>
<description>Current stage of the application</description>
<param-name>stage</param-name>
<param-value>registration</param-value>
</context-param>
I have an action which is redirect request for Payment Gateway i.e payment gateway is sending user to this action.
struts2.xml (demo entries)
<!-- for registration -->
<action name="paymentResponse" class="abc.xyz.PaymentResponse">
<result name="input" type="tiles">paymentForReg.tiles</result>
<result name="success" type="tiles">home.tiles</result>
</action>
<!-- for change data -->
<action name="paymentResponse" class="abc.xyz.PaymentResponseForChangeData">
<result name="input" type="tiles">paymentForChangeData.tiles</result>
<result name="success" type="tiles">home.tiles</result>
</action>
So when stage of the application changes, what I am doing is changes stage in web.xml and commenting one entry from of action from struts.xml
So to summarize I have multiple actions with same name and I want to trigger(or change class name of action) action on the basis of context param. is there any way to do it?
If i am not wrong i think you have to use dynamic action like below then you can use the same action and same entry in xml
struts.xml:
<action name="paymentResponse" class="abc.xyz.RedirectPaymentResponse" method="updateCriteria">
<result name="response" type="tiles">paymentForChangeData.tiles</result>
<result name="responsechanged" type="tiles">paymentForReg.tiles</result>
<result name="success" type="tiles">home.tiles</result>
</action>
Action class:
public class RedirectPaymentResponse extends AbstractAppAction {
public String execute () throws Exception
{
// some code
return "success";
}
public String updateCriteria(){
//logic here
if(PaymentResponse){
// code here
return "response";
}
if(PaymentResponseChanged){
// code here
return "responsechanged"
}
}
I am using struts2 for web development.
after deploying
it is showing url http://localhost:8084/iland/ which is login page of my site
which is correct but after login it is redirected to 'about' action which fetch user details so in this case url should be http://localhost:8084/iland/about,
but it is showing url http://localhost:8084/iland/pages/about.
Here 'pages' is included, due to this change my css are not get loaded.
How to resolve this
my struts.xml for above is
<package name="myprofile" extends="struts-default">
<action name="login" class="action.LoginAction">
<result name="success" type="redirect">about</result>
<result name="input">/pages/login.jsp</result>
</action>
<package name="default" extends="struts-default">
<action name="about" class="social.action.fetchBasicProfile">
<result name="success">/pages/profile/about.jsp</result>
<result name="input">/pages/profile/about.jsp</result>
<result name="login">/pages/pleaselogin.jsp</result>
</action>
</package
There must be problem with your namespace, so please check your struts.xml and change the namespace="pages" to namespace="/". this will resolve your issue. let me know if u find any difficulty
In my struts.xml I have the following configured in order to handle redirection of myhost.com/admin and myhost.com/admin/ correctly:
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<action name="admin">
<result type="redirectAction">admin/home</result>
</action>
<action name="admin/">
<result type="redirectAction">admin/home</result>
</action>
Is there a quicker way to do this in Struts2?
You are mixing two things: action and namespace. You can use the below config:
<action name="admin">
<result type="redirectAction">admin/</result>
</action>
<action name="" namespace="admin">
<result>/admin/index.jsp</result>
</action>
AS commented by yourself, have you tried the wildcard like this
admin/**
hope this helps
I am using Struts 2 + Hibernate (full-hibernate-plugin-for-struts2), and I have this code to check if image to upload is png:
struts.xml
<struts>
<package name="mypack" namespace="/" extends="hibernate-default">
...
<action name="myaction" class="com.actions.MyAction" >
<interceptor-ref name="defaultStackHibernate">
<param name="fileUpload.allowedTypes">image/png</param>
</interceptor-ref>
<result name="success" type="tiles" >baseLayout</result>
<result name="error" type="tiles" >error</result>
<result name="input" type="tiles" >baseLayout</result>
</action>
...
</package>
</struts>
The error message that comes out if I upload not allowed image types is:
Content-Type not allowed: image "img.jpg" "upload__2988a871_13b93535e21__7fc1_00000009.tmp" image/jpeg
How can I modify that error message? Using something like "hibernate-messages.properties"?
You can override this message by creating text for this key:
struts.messages.error.content.type.not.allowed
i have a problem, i have my login with Strits2, Tiles2 and Struts xml validations, when i run my webapp the first page is the login and the struts2 xml validator send me the error, how do i tell that for the first time do not validate until submit?
<action name="loginPage" class="com.webapp.login.action.LoginAction">
<result name="input" type="tiles">loginPage</result>
<result name="success" type="tiles">loginPage</result>
</action>
Whenever you want to go to a "blank form", you should execute the input() method of your Action. This tells Struts 2 that you do not want to perform any validation for the action:
<action name="loginPage" class="com.webapp.login.action.LoginAction"
method="input">
<result name="input" type="tiles">loginPage</result>
<result name="success" type="tiles">loginPage</result>
</action>
Then, you should submit your login form to another action definition that executes the default action execute(). Struts 2 will then try to perform Action validation:
<action name="loginPageSubmit" class="com.webapp.login.action.LoginAction">
<result name="input" type="tiles">loginPage</result>
<result name="success" type="tiles">loginPage</result>
</action>