Sie sind auf Seite 1von 9

Forward Action

Allow you to access the JSP class directly, without go through the controller class.
<action path="/loginAction" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/pages/login.jsp" />

Multiple Struts Configuration Files


<servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config-1.xml, /WEB-INF/struts-config-2.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>

Struts configuration Wildcard support


<action path="/*UserAction" type="com.mkyong.common.action.UserAction" parameter="{1}User"> <forward name="success" path="/pages/{1}User.jsp"/> </action>

Dispatch Action
Allow you to group all related functions into a single action class. Action class need to extends org.apache.struts.actions.DispatchAction class. <action path="/Locale" type="com.mkyong.common.action.LanguageSelectAction" name="userForm" parameter="method" validate="false"> <forward name="success" path="/multi-language.jsp" /> </action>

Add method parameter to request , to specify which action method to invoke.


public ActionForward chinese(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response)

{...}
public ActionForward english(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response)

{...}
<html:link page="/Locale.do?method=chinese">Chinese</html:link> <html:link page="/Locale.do?method=english">English</html:link>

MappingDispatchAction
Used to group similar functionality into a single action class, and execute the function depends on parameter attribute of the corresponding ActionMapping. <action path="/CustomDispatchActionXML" type="com.mkyong.common.action.MyCustomDispatchAction" parameter="generateXML"> (calls generateXML method of the action class) <forward name="success" path="/pages/DispatchExample.jsp"/> </action> <action path="/CustomDispatchActionExcel" type="com.mkyong.common.action.MyCustomDispatchAction" parameter="generateExcel"> (calls generateExcel method of the action class) <forward name="success" path="/pages/DispatchExample.jsp"/> </action>

LookUpDispatchAction DynaActionForm
The Struts DynaActionForm class is an interesting feature to let you create a form bean dynamically and declaratively. It enables you to create a virtual form bean in Struts configuration file instead of create a real Java form bean class. <form-bean name="dynaUserForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" type="java.lang.String"/> </form-bean>

Struts Localization
the two Internationalization (i18n) components that are packaged with the Struts Framework. The first of these components, which is managed by the application Controller, is a Message class that references a resource bundle containing Localedependent strings. The second Internationalization (i18n) component is a JSP custom tag, <bean:message />, which is used in the View layer to present the actual strings managed by the Controller.

All the localize messages are declared in properties file, format as filename_locale_code.properties. You can check the locale code in Java.Util.Locale class. e.g

English Common.properties or Common_en_US.properties Chinese Common_zh_CN.properties France Common_fr.properties German Common_de.properties

Common.properties In struts-config.xml specify : <message-resources parameter="Common" /> And set the locale from code , struts will pick up the appropriate properties file.

There are 2 ways to test this:1)by setting the lang. in IE:


Lets change the Language to French ("fr"). Go to Internet Explorer -> Tools -> Internet Options -> Click the Languages button -> Add the French language -> Move the French language to the first position. 2)setting the locale in struts action in session
request.getSession().setAttribute( Globals.LOCALE_KEY, Locale.SIMPLIFIED_CHINESE); request.getSession().setAttribute( Globals.LOCALE_KEY, Locale.GERMAN);

Struts Tiles
Struts tiles framework is a powerful layout framework, which is used to maintain a standard look of header, footer or menu details across all of your web pages.

Struts Validation
1)In Struts config add: <plug-in className="org.apache.struts.validator.ValidatorPlugIn" > <set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validator-user.xml"/> </plug-in> 2) For form bean need to use the validator framework, it have to extends the ValidatorForm, not ActionForm. 3) validator-user.xml <form-validation> <formset> <form name="userForm"> <field property="email" depends="required,email"> <msg name="required" key="err.user.email.required" /> <msg name="email" key="err.user.email.invalid" /> </field> ...... </form> </formset> </form-validation>

To show the error messages in alert use onsubmit: <html:form action="/registersubmit" onsubmit="return validateRegisterForm(this);">

<global-exception>
In Struts framework, the <global-exception> is used to display your custom error page, instead of the default classic HTTP Status 500 error page. In struts-config.xml add following: <global-exceptions> <exception key="error.global.mesage" type="java.io.IOException" path="/pages/error.jsp" /> </global-exceptions> In above, the default Struts exception handler org.apache.struts.action.ExceptionHandler will be called when any IOException is thrown by an Action, and forward it to error.jsp file. The key is a key in your message resources properties file. Custom Exception Handler: In the default exception handler, you have no way to control how to deal with the exception. In most cases, you may need to log the exception for further analysis. To do this, you need a custom exception handler to log all the exceptions to another data store like file system or database. Heres a custom exception handler example to configure for java.lang.Exception so that its called for any exception thrown by Action. To create a custom exception handler, you need to subclass org.apache.struts.action.ExceptionHandler and override the execute method. public class MyCustomExceptionHandler extends ExceptionHandler{ private static final Logger logger = Logger.getLogger(MyCustomExceptionHandler.class); @Override public ActionForward execute(Exception ex, ExceptionConfig ae, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response) throws ServletException { //log the error message logger.error(ex); return super.execute(ex, ae, mapping, formInstance, request, response); } } <global-exceptions> <exception key="error.global.mesage" type="java.io.IOException" handler="com.mkyong.common.exception.MyCustomExceptionHandler" path="/pages/error.jsp" /> </global-exceptions>

Struts Tag lib


Following are the taglibs provided by struts:

1)html 2)logic 3)bean 4)nested JSP standard tag library: 1)core 2)xml3)sql4)fmt(i18n) Struts 2.0 tag libs: 1) Generic tags (control tags and data tags) 2)UI tags (form ,non-form and ajax) Jsf tag libs: Spring tag libs:

Bean: 1)Bean:write: Retrieve the value of the specified bean property <bean:write name="msg" /> <bean:write name=loginform property=username/>

2)Bean:message : Retrieves an internationalized message for the specified locale, using the specified message key, and write it to the output stream. <bean:message key="label.username" /> bean:cookie bean:define bean:header bean:include bean:page bean:parameter bean:resource bean:size bean:struts

Html: 1)html:form : <html:form action="/login"> 2) html:messages : Displays a set of messages prepared by a business logic component and stored as an ActionMessages object, ActionErrors object, a String, or a String array in any scope. If such a bean is not found, nothing will be rendered. To display all Action message set in action: <html:messages id="msg" message="true"> <bean:write name="msg" /> <br> </html:messages>

ActionMessages messages = new ActionMessages(); ActionMessage msg = new ActionMessage("message.invalidunorpass"); messages.add("message1", msg); saveMessages(request, messages); To display all ActionError set in validate() method of form: <html:messages id="msg" message="false"> <bean:write name="msg" /> <br> </html:messages> ActionErrors errors = new ActionErrors(); errors.add("username.err",new ActionMessage("error.username.required")); There is a way to display individual message(TODO) 3)html:text : Render An Input Field of Type text <html:text property="userName"></html:text> 4)html:password: <html:password property="password"></html:password> 5)html:submit: <html:submit>Submit</html:submit> html:base html:button html:cancel html:checkbox html:errors html:file html:frame

html:hidden html:html html:image html:img html:javascript html:link html:multibox html:option html:options html:optionsCollection html:param html:radio html:reset html:rewrite html:select html:textarea html:xhtml Logic:

logic:empty logic:equal logic:forward logic:greaterEqual logic:greaterThan logic:iterate logic:lessEqual logic:lessThan logic:match logic:messagesNotPresent logic:messagesPresent logic:notEmpty logic:notEqual logic:notMatch logic:notPresent logic:present logic:redirect

Nested: nested:checkbox nested:define nested:empty

nested:equal nested:errors nested:file nested:form nested:greaterEqual nested:greaterThan nested:hidden nested:image nested:img nested:iterate nested:lessEqual nested:lessThan nested:link nested:match nested:message nested:messages nested:messagesNotPresent nested:messagesPresent nested:multibox nested:nest nested:notEmpty nested:notEqual nested:notMatch nested:notPresent nested:options nested:optionsCollection nested:password nested:present nested:radio nested:root nested:select nested:size nested:submit nested:text nested:textarea nested:write nested:writeNesting

Das könnte Ihnen auch gefallen