Struts Theme And Template
Posted on: February 9, 2011 at 12:00 AM
This this tutorial you will learn how to make a theme based application in struts

Struts Theme And Template Example

To make a theme based application in struts in need to add the following mapping in struts.xml file

<constant name="struts.ui.theme" value="themeDirectory" />
<constant name="struts.ui.templateDir" value="templateDirectory" />

A Theme based login application is given below. In this application the fieldError is displayed using the theme.

index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=login.action">
</head>
<body>
<p>Loading ...</p>
</body>
</html>

login.jspADS_TO_REPLACE_1

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts Theme Example</title>
<link href="<s:url value="style.css"/>" rel="stylesheet" type="text/css" />
</head>
<body bgcolor="lightblue">
<br>
<br>
<b><font color="#0000FF" face="modern" size="6">Please Login......</font></b>
<br>
<br>
<br>
<s:form action="doLogin" method="GET">
<table border=0 align="center">
<tr>
<td><s:textfield name="userName" label="User Name" /></td>
</tr>
<tr>
<td><s:password name="password" label="Password" /></td>
</tr>
<tr>
<td><s:submit align="center" value="login" /></td>
</tr>
</table>
</s:form>
</body>
</html>

home.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Home Page</title>
</head>
<body bgcolor="lightblue"><br><br><br><br><br>
<h1>Welcome </h1><s:property value="userName"/>
</body>
</html>

style.css

.errorsBg {
	background-color: #19FF19;
	color: red;
	border: 1px solid;
}

.errorMessage {
	padding: 4px 6px;
}

table {
	border-spacing: 6px;
}

td {
	padding: 5px;
}

LoginModel.javaADS_TO_REPLACE_2

package net.roseindia.model;

import java.io.Serializable;

public class LoginModel implements Serializable{

	private static final long serialVersionUID = 1L;
	private String userName;
	private String password;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

LoginAction.java

package net.roseindia.action;

import net.roseindia.model.LoginModel;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LoginAction extends ActionSupport implements
		ModelDriven<LoginModel> {

	private static final long serialVersionUID = 1L;
	LoginModel model;

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		if ("admin".equalsIgnoreCase(model.getPassword())) {
			return SUCCESS;
		}
		return INPUT;
	}

	@Override
	public void validate() {
		// TODO Auto-generated method stub
		if ("".equals(model.getUserName())) {
			addFieldError("userName", getText("userName"));

		}
		if ("".equals(model.getPassword())) {
			addFieldError("password", getText("password"));
		}
		super.validate();
	}

	@Override
	public LoginModel getModel() {
		// TODO Auto-generated method stub
		model = new LoginModel();
		return model;
	}

}

LoginAction.properties

userName = Please Enter Name
password = Password is 'admin'

SampleInterfaceImp.javaADS_TO_REPLACE_3

<?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.custom.i18n.resources" value="global" />
<constant name="struts.devMode" value="true" />

<constant name="struts.ui.theme" value="roseindia" />
<constant name="struts.ui.templateDir" value="template" />

<package name="roseindia" namespace="/" extends="struts-default">
<action name="login">
<result>resources/login.jsp</result>
</action>
<action name="doLogin" class="net.roseindia.action.LoginAction">
<result name="success">resources/home.jsp</result>
<result name="input">resources/login.jsp</result>
</action>
</package> 

</struts>


When you run this application it will display message as shown below:


 
 
 

Download Select Source Code

Related Tags for Struts Theme And Template :

Advertisements

Ads

 
Advertisement null

Ads