Friday, July 27, 2012

Struts2 Programs 004 Dispatcher Result

struts.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>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<package name="kites" extends="struts-default">

<action name="login">
<result>/login.jsp</result>
</action>

<!-- Redirect to another namespace -->

<action name="doLogin" class="com.kites.Login">
<result name="success" type="dispatcher">
<param name="location">/home.jsp</param>
</result>

<result name="error" type="dispatcher">
<param name="location">/error.jsp</param>
</result>

</action>

</package>

</struts>
-------------------------------------------------------------------------------------------------------------------------------

Login.java
-----------------

package com.kites;
import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport {

public String execute() throws Exception {

System.out.println("Login Action Called");
if(getUsername().equalsIgnoreCase("")|| getPassword().equalsIgnoreCase("")){

return ERROR;
}
else if(!getPassword().equals("Admin")){

return ERROR;
}else{

return SUCCESS;
}
}

private String username = null;

public String getUsername() {

return username;
}

public void setUsername(String value) {

username = value;
}

private String password = null;

public String getPassword() {

return password;
}

public void setPassword(String value) {

password = value;
}

}

---------------------------------------------------------------------------------------------------------------------
login.jsp
------------------
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Chain Result Example</title>

<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css"/>

</head>
<body>
<s:form action="doLogin" method="POST">
<tr>
<td colspan="2">
Login
</td>
</tr>
<tr>
<td colspan="2">
<s:actionerror />
<s:fielderror />
</td>
</tr>
<s:textfield name="username" label="Login name"/>
<s:password name="password" label="Password"/>
<s:submit value="Login" align="center"/>
</s:form>
</body>
</html>

-----------------------------------------------------------------------------------------------------------------------
home.jsp
---------------

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body >
<center>
<h1> Home Page</h1>
<font color=green size=15 face=modern> Welcome :<s:property value="username"/> </font><br>
</center>
</body>
</html>

--------------------------------------------------------------------------------------------------------------------------
error.jsp
-----------------
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body >
<center>
<h1> Error Page</h1>
</center>
</body>
</html>
---------------------------------------------------------------------------------------------------------------------------
web.xml
----------------
<?xml version="1.0" encoding="UTF-8"?>

<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

<display-name>Struts 2</display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<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>
</web-app>
----------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment