Tuesday 7 December 2010

Login Form (Spring MVC)

Buat Project seperti disini
Tambahkan link seperti ini :  <a href="login.htm">Login</a>

 Buat halaman (form) login


 beri nama login (engga perlu di beri ekstensi .jsp)

 Ubah login.jsp seperti berikut
 Buat class controler untuk login (loginController)

 Untuk melengkapi dapat di tekan ctrl+space secara bersamaan
 Klik tanda balon kuning untuk mengetahui error-nya (class userLogin belum ada diminta untuk buat kita abaikan saja karena kita akan membuat class login -nya)
 Buat class userLogin, kita tempatkan pada pakage domain( lihat cara buat pakage di di sini )
 Nama class userLogin (case sensitive)
 buat properti:
private String userLogin;
private String passwordLogin; kemudian klik kanan - Insert Code

 pilih Getter dan Setter
 Beri centang semua
 Hasilnya seperti ini
 masuk kembali ke loginController, klik pada balon kuning, pilih add import for ......
 masuk kembali ke login.jsp lengkapi seperti berikut
Masuk ke loginController kembali tambhkan script seperti berikut
Sebelum di jalankan tambahkan dulu file properties seperti di sini
Setelah itu klik menu RUN

 Akan tampil seperti berikut.  klik link login
Masukan user dan password setelah itu tekan tombol Submit


 Masuk ke netbean kembali lihat pada bagian bawahm, akan ditampilkan user dan password yang telah kita masukan. Pada bagian ini hanya untuk mengetahui apakah for login yang kita buat berjalan dengan baik atau tidak (belum ada modul pengecekan dari user dan password yang dimasukan)

Source silahkan ambil disini
berikut scrip lengkapnya

AplicationContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <!--bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />
   
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}" /-->
   
    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->


          <context:annotation-config/>
           <tx:annotation-driven/>
           <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
           p:dataSource-ref="dataSource"/>



    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="${jdbc.driver}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}"/>
   
</beans>
======================================

Diapacher-Servlet:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
   
    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
   
   
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />
   
    <!--
    The index controller.
    -->
      <context:component-scan base-package="id.co" />
       <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
  
</beans>

======================================

login.jsp

<%--
    Document   : login
    Created on : Dec 8, 2010, 9:38:12 AM
    Author     : mugi22
--%>

<%@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>Login Page</title>
    </head>
    <body>
        <h1>Login</h1>
        <form method="post">
        <table border="1">
            <tbody>
                <tr>
                    <td colspan="3">L O G I N</td>
                </tr>
                <tr>
                    <td>User</td>
                    <td>:</td>
                    <td><input type="text" name="userLogin" value="" /></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td>:</td>
                    <td><input type="password" name="passwordLogin" value="" /></td>
                </tr>
                <tr>
                    <td colspan="3"><input type="submit" value="Submit" />
                        <input type="reset" value="reset" />
                    </td>
                </tr>
            </tbody>
        </table>
        </form>
    </body>
</html>

==============================


loginController:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package id.co.myapp.controller;

import id.co.myapp.domain.userLogin;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author mugi22
 */
@Controller
@RequestMapping("/login.htm")
public class loginController {
    //bila halaman login di panggil
    @RequestMapping(method=RequestMethod.GET)
    public void loginPage(){
       
    }

    //proses halaman login
    @RequestMapping(method=RequestMethod.POST)
    public String loginProses(@ModelAttribute("userLogin")userLogin userLogin){
        System.out.println("User Login   :  "+userLogin.getUserLogin());
        System.out.println("User Password   :  "+userLogin.getPasswordLogin());
        return null;
    }
}

===================
userLogin.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package id.co.myapp.domain;

/**
 *
 * @author mugi22
 */
public class userLogin {
    private String userLogin;
    private String passwordLogin;

    public String getPasswordLogin() {
        return passwordLogin;
    }

    public void setPasswordLogin(String passwordLogin) {
        this.passwordLogin = passwordLogin;
    }

    public String getUserLogin() {
        return userLogin;
    }

    public void setUserLogin(String userLogin) {
        this.userLogin = userLogin;
    }


}

===============


index.jsp
<%@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>Welcome to Spring Web MVC project</title>
    </head>
   
    <body>
        <p>Hello! This is the default welcome page for a Spring Web MVC project.</p>
        <p><i>To display a different welcome page for this project, modify</i>
          <tt>index.jsp</tt> <i>, or create your own welcome page then change
          the redirection in</i> <tt>redirect.jsp</tt> <i>to point to the new
          welcome page and also update the welcome-file setting in</i>
          <tt>web.xml</tt>.</p>
        <a href="login.htm">Login</a>
    </body>
</html>

No comments:

Post a Comment