Example of Tiles application in struts2.0 framework
Previous | Home | Next |
Tiles implementation login example in struts2.0 framework.Directory structure of tiles sub framework in struts2.0 in eclipse IDEJava file:-r4r/src/mypack/LoginAction.javaStruts.xml:-r4r/src/struts.xmlWeb.xml:-r4r/web-root/WEB-INF/web.xmlTiles.xml:- r4r/web-root/WEB-INF/tiles.xmletc .
Directory Structure of Tiles Example in Struts 2.0 Using MyEclipse IDEindex.jsp<%@taglib uri="/struts-tags" prefix="s"%> <s:form action="login"> <s:textfield name="name" label="Name"/> <s:password name="password" label="Password"/> <s:submit value="Login"/> </s:form>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>f1</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>f1</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> </listener> </web-app>struts.xml<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="demo" extends="tiles-default"> <action name="login" class="mypack.LoginAction"> <result name="success" type="tiles">hello</result> <result name="failure" type="tiles">relogin</result> </action> </package> </struts>tiles.xml<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "http://struts.apache.org/dtds/tiles-config_2_0.dtd"> <tiles-definitions> <definition name="hello" template="/MyLayout.jsp"> <put-attribute name="title" value="Welcome Page"/> <put-attribute name="header" value="/header.jsp"/> <put-attribute name="footer" value="/footer.jsp"/> <put-attribute name="content" value="/hello.jsp"/> </definition> <definition name="relogin" template="/MyLayout.jsp"> <put-attribute name="title" value="Login Error"/> <put-attribute name="header" value="/header.jsp"/> <put-attribute name="footer" value="/footer.jsp"/> <put-attribute name="content" value="/relogin.jsp"/> </definition> </tiles-definitions>LoginAction.javapackage mypack; public class LoginAction { String name,password; public String execute(){ UserDao dao=new UserDao(); if(dao.findUser(name,password)) return "success"; else return "failure"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }UserDao.javapackage mypack; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class UserDao { public boolean findUser(String name,String password) { try{ Connection con=ConnectionProvider.getConnection(); System.out.println("connection con====="+con); PreparedStatement stmt=con.prepareStatement("select * from userInfo where name=? and password=?" ); stmt.setString(1,name); stmt.setString(2,password); ResultSet rset=stmt.executeQuery(); if(rset.next()) return true; else return false; }catch(Exception e) { System.out.println(e); } return false; } }ConnectionProvider.javapackage mypack; import java.sql.Connection; import java.sql.DriverManager; public class ConnectionProvider { public static Connection getConnection() { Connection con=null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system"); }catch(Exception e) { System.out.println(e); } return con; } }header.jsp<html> <head></head> <body><center> <H1><font color="red">Welcome Mukund Web site</font></H1> </center></body></html>footer.jsp<html> <head></head> <body><center><h1><font color="red"> This web site is used only GOOD student </font></h1></center></body></html>hello.jsp<%@taglib uri="/struts-tags" prefix="s"%> <b>Welcome, <s:property value="name"/></b>MyLayout.jsp<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%> <html> <head><title><tiles:getAsString name="title"/></title></head> <body> <table width="100%" height="100%" border="1" bgcolor="c0c0c0"> <tr><td width="100%" height="10%"> <tiles:insertAttribute name="header"/> </td></tr> <tr><td width="100%" height="80%"> <tiles:insertAttribute name="content"/> </td></tr> <tr><td width="100%" height="10%"> <tiles:insertAttribute name="footer"/> </td></tr> </table> </body> </html>relogin.jsp<html><head></head><body><center><font color="red"> <b>Invalid UserName and Password !</b> <jsp:include page="index.jsp"/></font></center></body></html>
Previous | Home | Next |