Friday, June 1, 2012

පළමු Struts2 Web application එක ලියමු - User ට Hello කියමු.

Struts2 Framework එක භාවිතයෙන් සරල application එකක් ලියන හැටි කියන්නයි මේ ලිපිය ලියන්නේ.

 Web application එකක් පිළිබඳව මූලික දැනීමක් ඇතැයි අපේක්ෂා කරනවා. මන්ද හැමදෙයම මුලසිට සරලව විස්තර කිරීම අපහසු නිසා. මේ ලිපියේ කරුණු ගැන ගැටළුවක් ඇත්නම් විමසන්න comment තීරුව භාවිතා කරන්න. එලෙසම යම් වරදක් ඇතොත් පෙන්වා දෙන්න අමතක කරන්නත් එපා.

මේ සඳහා ලිවිය යුතු files මේවාය.
 1) Logon.jsp
 2) Struts.xml
 3)  LoginAction.java
 4)  ApplicationResource.properties
 5) Welcome.jsp
 6)  Web.xml
 

එක් එක් file වල කාර්යභාරය කෙටියෙන්...

 1) Logon.jsp
    -User ගෙන් අවශ්‍ය input ලබාගැනීම සඳහා
  
 2) Struts.xml
    -  Struts හි Configurations  සඳහා..
        request එකට අදාළ action class එක සහ method එක සොයාගැනීම
        response එකට අනුව display කළයුතු page එක සොයාගැනීම

 3)  LoginAction.java
    -Business logic එක ඇත්තේ මෙහිය.
     ලබාගත් input, variable වලට assign වන්නේ මෙහිදීය. 

 4)  ApplicationResource.properties
     වෙනම properties file එකක් යොදා ගැනීමෙන් labels සහ error messages ලබාදීම පහසු කරගත හැක.

 5) Welcome.jsp
     - Final Output එක display කරන page එක


 6)  Web.xml
    - Web application එකේ configurations  ලබාදීම සඳහා
     

ඊළඟට code සහිතව සහ වඩා විස්තරාත්මකව සළකා බලමු.



 1) Logon.jsp

මෙය Login JSP Page එකයි.

01. <%@ page contentType="text/html; charset=UTF-8"%>
02. <%@ taglib prefix="s" uri="/struts-tags"%>
03. <html>
04. <head>
05. <title>Struts 2 - Login Application | PMTDB </title>
06. </head>
07.
08. <body>
09. <h2>Struts 2 - Login Application</h2>
10. <s:actionerror />
11. <s:form action="login.action" method="post">
12.    <s:textfield name="username" key="label.username" size="20" />
13.    <s:password name="password" key="label.password" size="20" />
14.    <s:submit method="execute" key="label.login" align="center" />
15. </s:form>
16. </body>
17. </html>



Web Application එක Run වෙනවිට මුලින්ම දර්ශනය වන්නේ මේ පිටුවයි.
මෙහිදී User ගෙන් username සහ password ලබාගැනෙයි. Login Button එක Click කළ විට Login request එකක් generate වෙයි. මේ request එක action class එකකට ගොස් අදාළ conditions පරීක්ෂා කළ යුතුය.

ඉතින් මේ request එක යා යුතු action class එක කුමක්දැයි සොයාගන්නේ කෙසේද? එය සොයාදෙන්නේ Struts.xml file එකයි.

2) Struts.xml


 01. <?xml version="1.0" encoding="UTF-8" ?>
02.   <!DOCTYPE struts PUBLIC
03.    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04.   "http://struts.apache.org/dtds/struts-2.0.dtd">

05. <struts>
06.    <constant name="struts.enable.DynamicMethodInvocation"
07.        value="false" />
08.    <constant name="struts.devMode" value="false" />
09.    <constant name="struts.custom.i18n.resources"
10.        value="ApplicationResources" />

11.     <package name="default" extends="struts-default" namespace="/">
12.        <action name="login" method="execute"
13.            class="PMTDB.struts2.LoginAction">
14.            <result name="success">Welcome.jsp</result>
15.            <result name="error">Login.jsp</result>
16.        </action>
17.        </package>
18. </struts>






action name එක Login නම් යා යුතු Action class එක LoginAction බවද ක්‍රියාත්මක විය යුතු method එක    execute බවද ඉහත file එකේ සඳහන් වෙයි.

එපමණක් නොව method එක execute වීමේදී ලැබෙන result එක අනුව ඊළඟට යා යුතු pages සඳහන් වෙන්නේ ද මෙහිමය. result එක success නම් Welcome.jsp page එකට යා යුතු බවද result එක error නම් නැවත Login.jsp page එකට යා යුතු බවද ඉහත සඳහන් වී ඇත.

 3)  LoginAction.java

           package PMTDB.struts2;

                import com.opensymphony.xwork2.ActionSupport;

               public class LoginAction extends ActionSupport {
                       private String username;
                       private String password;

                  public String execute() {

                       if (this.username.equals("PQR")
                          && this.password.equals("PQR123")) {
                          return "success";
                     } else {
                         addActionError(getText("error.login"));
                       return "error";
                      }
                       }

                  public String getUsername() {
                    return username;
                   }

                   public void setUsername(String username) {
                  this.username = username;
                  }

                  public String getPassword() {
                  return password;
                    }

                 public void setPassword(String password) {
                 this.password = password;
                    }
                  }





Action class එකේදී username සහ password යන properties, declare කර ඇත. එලෙසම ඒවාට getters සහ setters ලියා ඇත.

 Action class එක ActionSupport නම් class එක inherit කර ඇත.

 public class LoginAction extends ActionSupport {

 execute method එක තුළ username සහ password, check කර ඇත. username යන්න PQR නම් සහ password යන්න PQR123 නම් success යන්නද නැත්නම් error යන්නද return කර ඇත.

 4)  ApplicationResource.properties

ApplicationResources වෙනම properties file එකක් යොදා ගැනීමෙන් labels සහ error messages ලබාදීම පහසු කරගත හැක.


label.username= Username
label.password= Password
label.login= Login
error.login= Invalid Username/Password. Please try again.



5) Welcome.jsp

Action class එකේදී success return වුනොත් පෙන්නුම් කරන්නේ පහත jsp එකයි.

01. <%@ page contentType="text/html; charset=UTF-8"%>
02. <%@ taglib prefix="s" uri="/struts-tags"%>
03. <html>
04. <head>
05. <title>Welcome</title>
06. </head>
07.
08. <body>
09.     <h2>Hello, <s:property value="username" />.......!</h2>
10. </body>
11. </html>



මෙහිදී කලින් ලබාගත් username එක යොදාගෙන user ට Hello කියා ඇත.

6)  Web.xml

පහත ලබාදී ඇත්තේ configurations සඳහා වන Web.xml එකයි.

01. <?xml version="1.0" encoding="UTF-8"?>
02. <web-app id="WebApp_9" version="2.4"
03.    xmlns="http://java.sun.com/xml/ns/j2ee"
04.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05.    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
06.
07.    <display-name>Struts2 Application | PMTDB </display-name>
08.    <filter>
09.        <filter-name>struts2</filter-name>
10.        <filter-class>
11.            org.apache.struts2.dispatcher.FilterDispatcher
12.        </filter-class>
13.    </filter>
14.    <filter-mapping>
15.        <filter-name>struts2</filter-name>
16.        <url-pattern>/*</url-pattern>
17.    </filter-mapping>
18.    <welcome-file-list>
19.        <welcome-file>Login.jsp</welcome-file>
20.    </welcome-file-list>
21.
22. </web-app>





2 comments: