17 October 2016

Struts Framework: Login Page Example Basic


=>> Login success username: abc  password: 123

=>> login wrong username: abcd  password: 1234
HOME.jsp
Java Web  2016
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:form action="/login">
    Enter name<html:text property="uname"/>
    <br/>
    password:<html:password property="password"/>
    <br/>
    <html:submit property="ok"/>
</html:form>
invalid.jsp
Java Web  2016
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Invalid</title>
    </head>
    <body>
        <h1>Dang nhap that bai!</h1>
        <a href="HOME.jsp">try again</a>
    </body>
</html>
valid.jsp
Java Web  2016
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Valid</title>
    </head>
    <body>
        <h1>Dang nhap thanh cong!</h1>
    </body>
</html>
NewStrutsAction.java
Java Web  2016
public class NewStrutsAction extends org.apache.struts.action.Action {

    private static final String SUCCESS = "success";
    private static final String FAIL = "fail";

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        NewStrutsActionForm ob = (NewStrutsActionForm)form;

        String n = ob.getUname();
        String p = ob.getPassword();

        if (n.equals("abc") && p.equals("123")) {

            return mapping.findForward(SUCCESS);
        } else {

            return mapping.findForward(FAIL);
        }
    }
}
ModelList.jsp
Java Web  2016
package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Lonely
 */
public class NewStrutsActionForm extends org.apache.struts.action.ActionForm {
    
    private String name,uname,password;

     /*Setter and Getter uname, password*/
     public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPassword() {
        return password;
    }

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

    private int number;

    public String getName() {
        return name;
    }

    public void setName(String string) {
        name = string;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int i) {
        number = i;
    }

    public NewStrutsActionForm() {
        super();
        // TODO Auto-generated constructor stub
    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getName() == null || getName().length() < 1) {
            errors.add("name", new ActionMessage("error.name.required"));
            // TODO: add 'error.name.required' key to your resources
        }
        return errors;
    }
}
struts-config.xml
Java Web  2016
<global-forwards>
    <forward name="fail" path="/invalid.jsp"/>
    <forward name="success" path="/valid.jsp"/>
    <forward name="welcome"  path="/Welcome.do"/>
</global-forwards>
struts SIMPLE LOGIN PAGE

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang