Struts2 - Converting struts xml to annotation - struts2

I have defined the type=steam, but its throwing error for below annotation.
#Result(name="success",type="stream" ,params={"contentType","text/html","inputName","inputStream"}).
struts.xml
<package name="default" extends="struts-default">
<action name="getJSON" class="com.Action" method="list">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
</package>
How to define action name and method in annotation ?
Already i have one annotation, so i have added results ..
#Results({#Result(name=com.action.Part.INPUT, value="/search.jsp",
type= ServletDispatcherResult.class, params={"location", "/search.jsp" }),
#Result(name = ActionSupport.SUCCESS,type="stream",params= {"contentType","text/html","inputName","inputStream"})})

Because have imported import org.apache.struts2.config.Result; instead of import org.apache.struts2.convension.annotation.Result

Related

Attribute parameter from <action> element - equivalent in Struts 2

In Struts1 you can use the attribute parameter from element(struts-config.xml) and access it's value within the action class via the actionMapping.getParameter() method. For actions requiring multiple steps, the parameter is often used to indicate which step the mapping is associated
with.
Ex:
<action path="\something\Step1"
type="actions.SomethingAction"
parameter="step1"> ...
<action path="\something\Step2"
type="actions.SomethingAction"
parameter="step2"> ...
Which is the alternative solution for Struts2?
Parameters in the action configuration could be used instead
<package name="something" namespace="/something" extends="struts-default">
<action name="Step1" class="actions.SomethingAction">
<param name="step1" ...
</action>
<action name="Step2" class="actions.SomethingAction">
<param name="step2" ...
</action>
</package>

Strange effects when chaining results in struts 2

When an error event occurs I am chaining the result to an error action, this works okay when its all happening in the same namespace but when chaining across namespaces the results of the chained action are in the chaining actions namespace.
<package name="mynamespace-htm" namespace="/mynamespace/htm" extends="mybase-base">
<action name="newsItem" class="com.mypackage.control.GetNewsItemAction">
<result name="success">newsItem.jsp</result>
<result name="error" type="chain">
<param name="actionName">invalidSite</param>
<param name="namespace">/mynamespace</param>
</result>
</action>
</package>
<package name="mynamespace" namespace="/mynamespace" extends="mybase-base">
<action name="invalidSite" class="com.mypackage.control.GetInvalidSiteAction">
<result name="success">commonviews/invalidSite.jsp</result>
</action>
</package>
If I access the newsItem action in a way that causes an error result I get an exception javax.servlet.ServletException: File "/mynamespace/htm/commonviews/invalidSite.jsp" not found why is it trying to find the file relative to the original namespace mynamespace/htm instead of mynamespace?

Action Message and action Error is not forwarding to the different action in struts2.3.12

I am using latest struts 2.3.12 version.
My struts.xml
<action name="*ContractorDetail" class="waid.config.contractor.ContractorDetailAction" method="{1}">
<result name="ContractorDetailScreen" type="tiles">ShowContractorDetailScreen</result>
<result type="chain" name="success">
<param name="actionName">ContractorSummary</param>
<param name="namespace">/app</param>
</result>
<result name="input" type="redirectAction">
<param name="actionName">ContractorSummary</param>
<param name="namespace">/app</param>
</result>
<result name="error" type="redirectAction">
<param name="actionName">ContractorSummary</param>
<param name="namespace">/app</param>
</result>
<result name="deleteDependency" type="chain">
<param name="actionName">ContractorSummary</param>
<param name="namespace">/app</param>
</result>
</action>
<action name="*ContractorSubmit" class="waid.config.contractor.ContractorSubmitAction" method="{1}">
<result type="chain" name="success">
<param name="actionName">modifyContractorDetail</param>
<param name="namespace">/app</param>
</result>
<result name="error" type="tiles">ShowContractorDetailScreen</result>
<result name="input" type="tiles">ShowContractorDetailScreen</result>
</action>
In my Submit action i am setting actionMessage and action errorMessage which will forwarded to detailsAction.
In previous version of struts(2.1) action message are showing in jsp.
But after upgrading to struts 2.3.12 version action message and action error both are not coming in jsp.
Try add these lines in struts.xml to restore the original behaviour
<constant name="struts.xwork.chaining.copyErrors" value="true"/>
<constant name="struts.xwork.chaining.copyFieldErrors" value="true"/>
<constant name="struts.xwork.chaining.copyMessages" value="true"/>
I took the solution from here:
http://struts.apache.org/release/2.3.x/docs/chaining-interceptor.html

Change Struts2 interceptor error messages

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

How to use Struts 2 with JFreeChart?

Firstly, I went here ( http://code.google.com/p/struts2-examples/downloads/list and I downloaded Hello_World_Struts2_Mvn.zip) and I run that example.
After that, I went here (http://struts.apache.org/2.x/docs/jfreechart-plugin.html), I add the dependencies for commons-lang-2.5.jar, jcommon-1.0.16.jar and jfreechart-1.0.13.jar and I modify the example downloaded from code.google.com to see how JFreeChart is working, but I receive this error:
Unable to load configuration. - action - file:/C:/.../untitled_war_exploded/WEB-INF/classes/struts.xml:34:67
Caused by: Error building results for action createChart in namespace - action - file:/C:/.../out/artifacts/untitled_war_exploded/WEB-INF/classes/struts.xml:34:67
Caused by: There is no result type defined for type 'chart' mapped with name 'success'. Did you mean 'chart'? - result - file:/C:/.../out/artifacts/untitled_war_exploded/WEB-INF/classes/struts.xml:36:49
At the line 36 in struts.xml is the this code (the code from struts2 website):
<action name="viewModerationChart" class="myapp.actions.ViewModerationChartAction">
<result name="success" type="chart">
<param name="width">400</param>
<param name="height">300</param>
</result>
</action>
What I'm doing wrong?
You need to define Action mappings related to chart differently in Struts.xml.Change your project's struts.xml with Jfreechart related actions and define it in separate package.
For eg.
<package name="struts2" extends="jfreechart-default">
<action name="viewModerationChart" class="myapp.actions.ViewModerationChartAction">
<result name="success" type="chart">
<param name="width">400</param>
<param name="height">300</param>
</result>
</action>
</package>

Resources