
index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>RoseIndia.net Struts Tutorials</title> </head> <body> <p>Browser the following link to run the example</p> <b>Hello World Example</b><br> <a href="roseindia/HelloWorld.action?request_locale=en">Hello World in English</a><br> <a href="roseindia/HelloWorld.action?request_locale=es">Hello World in Espanol</a><br> <br> <b>Form Examples</b><br> <a href="roseindia/showLogin.action">Login Form</a> </body> </html>
Save the index.html into struts221 application
HelloWorld.jsp
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title><s:text name="HelloWorld.message"/></title> </head> <body> <h2><s:property value="message"/></h2> <br> <a href="/struts221/">Back to Index page</a> </body> </html>login.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Struts 2 Login Application!</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> <br> <a href="/struts221/">Back to Index page</a> </body> </html>loginsuccess.jsp
<html> <head> <title>Login Success</title> </head> <body> <p align="center"><font color="#000080" size="5">Login Successful</font></p> <br> <a href="/struts221/">Back to Index page</a> </body> </html>
HelloWorld.java
package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
public String execute() throws Exception {
setMessage(getText(MESSAGE));
System.out.println("\nHello World Action Called");
return SUCCESS;
}
public static final String MESSAGE = "HelloWorld.message";
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Save the HelloWorld.java file into roseindia directory.Login.java
package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class Login extends ActionSupport {
public String execute() throws Exception {
System.out.println("Login Action Called");
if(!getUsername().equals("Admin") || !getPassword().equals("Admin")){
addActionError(getText("Login.invalidLogin"));
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;
}
}
Save the Login.java file into roseindia directory.package.properties
HelloWorld.message= This Is Hello World
Login.invalidLogin=Invalid user name or password! Please try again!
requiredstring = ${getText(fieldName)} is required.
package_es.properties
HelloWorld.message= Hola a todos
Login.invalidLogin= Invalid login name
requiredstring = ${getText(fieldName)} is required.
Now configure the struts.xml file as
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="roseindia" namespace="/roseindia" extends="struts-default"> <action name="HelloWorld" class="net.roseindia.HelloWorld"> <result>/jsp/HelloWorld.jsp</result> </action> <!-- Login Form Configuration --> <action name="showLogin"> <result>/jsp/login.jsp</result> </action> <action name="doLogin" class="net.roseindia.Login"> <result name="input">/jsp/login.jsp</result> <result name="error">/jsp/login.jsp</result> <result>/jsp/loginsuccess.jsp</result> </action> </package> </struts>
Login-validation.xml
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="username"> <field-validator type="requiredstring"> <message key="requiredstring"/> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message key="requiredstring"/> </field-validator> </field> </validators>Save this struts.xml into the java directory and Login-validation.xml into roseindia directory and then configure the buld.xml file as
<project name="Struts 2 Tutorial" basedir="../" default="all">
<!-- Project settings -->
<property name="project.jar.file" value="HelloWorldExample.jar"/>
<path id="class.path">
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<fileset dir="libext">
<include name="**/*.jar"/>
</fileset>
</path>
<!-- Classpath for Project -->
<path id="compile.classpath">
<pathelement path ="lib/commons-beanutils.jar"/>
<pathelement path ="lib/commons-digester.jar"/>
<pathelement path ="lib/struts.jar"/>
<pathelement path ="libext/servlet-api.jar"/>
<pathelement path ="libext/catalina-ant.jar"/>
<pathelement path ="classes"/>
<pathelement path ="${classpath}"/>
</path>
<!-- Check timestamp on files -->
<target name="prepare">
<tstamp/>
<!--
<copy
file="src/java/struts.xml"
todir="src/classes"/>
-->
</target>
<!-- Copy any resource or configuration files -->
<target name="resources">
<copy todir="\classes" includeEmptyDirs="no">
<fileset dir="src/java">
<patternset>
<include name="**/*.conf"/>
<include name="**/*.properties"/>
<include name="**/*.xml"/>
</patternset>
</fileset>
</copy>
</target>
<!-- Normal build of application -->
<target name="compile" depends="prepare,resources">
<javac srcdir="src" destdir="src/classes" debug="true" debuglevel="lines,vars,source">
<classpath refid="class.path"/>
</javac>
<jar
jarfile="lib/${project.jar.file}"
basedir="src/classes"/>
</target>
<!-- Remove classes directory for clean build -->
<target name="clean"
description="Prepare for clean build">
<delete dir="src/classes"/>
<mkdir dir="src/classes"/>
</target>
<!-- Build entire project -->
<target name="project" depends="clean,prepare,compile"/>
<!-- Build project and create distribution-->
<target name="all" depends="project"/>
</project>
and save into the src directory
|
|
|
![]() |
![]() |