18 March, 2012

struts2 notes


difference between struts1 and struts2

Struts1
Struts2
action class extends the abstract base class
Action class implements ActionSupport
Actions are singleton
Action objects are instantiated for each request.
Actions are dependent on the servlet API
Independent of servlet API
testing the application is tough as execute method exposes the Servlet API
Easier
JSTL EL
OGNL
binds objects into the page context by using the standard JSP mechanism.
uses a ValueStack technology
Commons-Beanutils are used by used by Struts 1 for type conversion
OGNL used for type conversion


Q:How to do validations in struts2?

Ans: It can be done in following ways:
1) ActionClass-Validation.xml
<validators>
<field name="userName">
<field-validator type="requiredstring">
<message>User Name is required.</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message key="password.required" />
</field-validator>
</field>
</validators>

2) override validate method in Action Class.

public void validate() {
(getUserName().length() == 0) {
addFieldError("userName", "User Name is required");
} else if (!getUserName().equals("Eswar")) {
addFieldError("userName", "Invalid User");
}
if (getPassword().length() == 0) {
addFieldError("password", getText("password.required"));
}
}

Q: Exception handling in struts2?
Ans: It can be done in one of the following ways:
1) Global Exception Handling:

<global-exception-mappings>
<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="securityerror" />
            <exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
<global-results>
        <result name="securityerror">/securityerror.jsp</result>
            <result name="error">/error.jsp</result>
        </global-results>
  
global exception mapping node tells the Struts 2 framework what to do if an uncaught exception of the type specified (or a child of that type) is thrown by the the application.
global results mapping node relates the result value to a specific view page.

  2)Exception Handling Per Action:
  
<action name="actionspecificexception" class="org.apache.struts.register.action.Register" method="throwSecurityException">
           <exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException"
           <result>/register.jsp</result>
           <result name="login">/login.jsp</result>
</action>

3)Logging Exceptions:
   three parameter values you can set to enable logging (logEnabled), the log level to use (logLevel),and the log category (logCategory).
      <interceptors>
           <interceptor-stack name="appDefaultStack">
                <interceptor-ref name="defaultStack">
                    <param name="exception.logEnabled">true</param>
                    <param name="exception.logLevel">ERROR</param>
                 </interceptor-ref>
            </interceptor-stack>
       </interceptors>

We can set this interceptor in the default interceptor as:
<default-interceptor-ref name="appDefaultStack" />


1 comment: