Buat project baru, langkah2nya lihat posting sebelumnya :)
1. Web.xml
[sourcecode language="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>
[/sourcecode]
2.Struts.xml
[sourcecode language="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>
[/sourcecode]
3. example.xml
[sourcecode language="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>
[/sourcecode]
4. Login.jsp
[sourcecode language="html"]
<%--
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>
[/sourcecode]
5. Sukses.jsp
[sourcecode language="html"]
<%--
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>
[/sourcecode]
6. Login.java
[sourcecode language="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;
}
}
[/sourcecode]
Deploy dalam glassfish 3.1.1
Hasil:
Tampilan Awal
Tampilan Error
Tampilan Sukses
Selesai :)
No comments:
Post a Comment