Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Login Example


In the previous example, we have simply integrated the spring application with struts 2. Now let’s develop a login application with struts 2 and spring frameworks.

It is the simple example of login application without database and session management. If you need to apply the database interactivity and session management with this example, visit this link Login and Logout example in struts 2.

Example of Login application Spring and Struts2 Integration

You need to create following files for simple login application using spring and struts 2 application:

  1. index.jsp
  2. web.xml
  3. struts.xml
  4. applicationContext.xml
  5. Login.java
  6. login_success.jsp
  7. login_error.jsp

1) index.jsp

This page gets the name and password from the user.

  1. <%@ taglib uri=“/struts-tags” prefix=“s”%>
  2. <s:form action=“login”>
  3. <s:textfield name=“name” label=“Username”></s:textfield>
  4. <s:password name=“password” label=“Password”></s:password>
  5. <s:submit value=“login”></s:submit>
  6. </s:form>

2) web.xml

It defines controller class for struts 2 and ContextLoaderListener listener class to make connection between struts2 and spring application.

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <web-app version=“2.5”
  3.     xmlns=“http://java.sun.com/xml/ns/javaee”
  4.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  5.     xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee 
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
  7.   <welcome-file-list>
  8.     <welcome-file>index.jsp</welcome-file>
  9.   </welcome-file-list>
  10.   <filter>
  11.     <filter-name>struts2</filter-name>
  12.     <filter-class>
  13.         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  14.     </filter-class>
  15.   </filter>
  16. <listener>
  17. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  18. </listener>
  19.   <filter-mapping>
  20.     <filter-name>struts2</filter-name>
  21.     <url-pattern>/*</url-pattern>
  22.   </filter-mapping>
  23.   </web-app>

3) struts.xml

It defines the package with action and result. Here, the action class name is login which will be searched in the applicationContext.xml file.

  1. <?xml version=“1.0” encoding=“UTF-8” ?>
  2. <!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.1//EN”
  3. “http://struts.apache.org/dtds/struts-2.1.dtd”>
  4. <struts>
  5. <package name=“abc” extends=“struts-default”>
  6. <action name=“login” class=“login”>
  7. <result name=“success”>login_success.jsp</result>
  8. <result name=“error”>login_error.jsp</result>
  9. </action>
  10. </package>
  11. </struts>

4) applicationContext.xml

It defines a bean with id login. This beans corresponds to the com.Kreationnext.Login class. It will be considered as the action class here.

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans
  3.     xmlns=“http://www.springframework.org/schema/beans”
  4.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  5.     xmlns:p=“http://www.springframework.org/schema/p”
  6.     xsi:schemaLocation=”http://www.springframework.org/schema/beans 
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>
  8. <bean id=“login” class=“com.Kreationnext.Login”>
  9. <property name=“successmessage” value=“You are successfully logged in!”></property>
  10. <property name=“errormessage” value=“Sorry, username or password error!”></property>
  11. </bean>
  12. </beans>

5) Login.java

It defines four properties name, password, successmessage and errormessage; with execute method where success is returned if password is Kreationnext.

  1. package com.Kreationnext;
  2. public class Login {
  3. private String name,password,successmessage,errormessage;
  4. //setters and getters
  5. public String execute(){
  6.     if(password.equals(“Kreationnext”)){
  7.         return “success”;
  8.     }
  9.     else{
  10.         return “error”;
  11.     }
  12. }
  13. }

6) login_success.jsp

It prints values of userName and message properties.

  1. <%@ taglib uri=“/struts-tags” prefix=“s”%>
  2. ${successmessage}
  3. <br/>
  4. Welcome, <s:property value=“name”/><br/>

7) login_error.jsp

It is the error page. But it is not required in this example because we are not defining any logic in the execute method of action class.

  1. ${errormessage}
  2. <jsp:include page=“index.jsp”></jsp:include>

Comments

comments