Hello Dunia struts 2 netbeans 7.1

Netbeans 7.1

Buat project baru, langkah2nya lihat posting sebelumnya🙂

1. Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>example/Login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

2.Struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <include file="example.xml"/>
    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">
    </package>
</struts>

3. example.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="example" namespace="/example" extends="struts-default">
        <action name="HelloWorld" class="example.HelloWorld">
            <result>/example/HelloWorld.jsp</result>
        </action>
        
        <action name="Login" class="example.Login">
            <result name="success">/example/Sukses.jsp</result>
            
            <result name="input">/example/Login.jsp</result>
        </action>
        
    </package>
</struts>


4. Login.jsp

<%-- 
  Document   : Login
  Created on : Jan 13, 2012, 2:35:06 PM
  Author     : root
--%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
  </head>
  <body>
    <h2>Login</h2>
    
    <s:form action="Login">
      <s:actionerror/>
      <s:actionmessage/>
    <table>
        <tr>
          <td><s:textfield name="username" label="Username"/></td>
        </tr>
        <tr>
          <td><s:password name="password" label="Password"/></td>
        </tr>
        <tr>
          <td><s:submit value="Login"/></td>
        </tr>
    </table>
    </s:form>
  </body>
</html>

5. Sukses.jsp

<%-- 
  Document   : Sukses
  Created on : Jan 13, 2012, 2:31:08 PM
  Author     : root
--%>

<%@taglib prefix="s" uri="/struts-tags" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Sukses</title>
  </head>
  <body>
    Hallo <b><s:property value="username"/></b>
    <br/>
    <s:url value="/" var="loginVar"/>
    <s:a href="%{loginVar}">[ Back ]</s:a>
    
  </body>
</html>

6. Login.java

package example;

import com.opensymphony.xwork2.ActionSupport;

/**
 *
 * @author root
 */
public class Login extends ActionSupport {

  private static final long serialVersionUID = 1L;

  private String username;

  private String password;

  @Override
  public String execute() throws Exception {
     return super.execute();
  }

  @Override
  public void validate() {
    super.validate();

    if (getUsername().equalsIgnoreCase("") || getPassword().equalsIgnoreCase("")){
      addActionError("Username dan Password harus diisi");
    }

    if (!(getUsername().equalsIgnoreCase("admin") && (getPassword().equalsIgnoreCase("adminadmin")))){
      addActionError("Username dan Password tidak cocok");
    }

  }

  public String getPassword() {
    return password;
  }

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

  public String getUsername() {
    return username;
  }

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

}

Deploy dalam glassfish 3.1.1

Hasil:
Tampilan Awal

Tampilan Error

Tampilan Sukses

Selesai🙂

Published by

saifiahmada

Begin a big Process with a bit trying

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s