Spring4 MVC WebFlow Basic2 Project

Click here to download eclipse supported ZIP file



This is index.html JSP file and it is used display the output for the application.



<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="/loginFlow">click here to login</a>
</body>
</html>


This is create_new_account.jsp JSP file and it is used display the output for the application.



<%@ page isELIgnored="false"%>
<html xmlns:form="http://www.springframework.org/tags/form">
<body>
<form action="${flowExecutionUrl}&_eventId=accountInformationEntered" method="post">
<input type="hidden" name="_flowExecutionKey"
value="${flowExecutionKey}" /> <br /> <br /> Use any
other login name and password for login error. <br /> <br />
<table>
<tr>
<td>New Login Name:</td>
<td><input type="text" name="loginName" /></td>
</tr>
<tr>
<td>Enter Password:</td>
<td><input type="text" name="password" /></td>
</tr>
</table>
<br /> <input type="submit" value="Submit" />
</form>
</body>
</html>


This is display_login.jsp JSP file and it is used display the output for the application.



<%@ page isELIgnored="false"%>
<html xmlns:form="http://www.springframework.org/tags/form">
<body>
<form action="${flowExecutionUrl}&_eventId=loginCredentialsEntered" method="post">

<input type="hidden" name="_flowExecutionKey"
value="${flowExecutionKey}" /> <br /> Enter login name as <b>Chandra</b>
and password as <b>password</b> for successful login. <br /> Use any
other login name and password for login error. <br /> <br />
<table>
<tr>
<td>Login Name:</td>
<td><input type="text" name="loginName" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" name="password" /></td>
</tr>
<%-- <tr>
<td>Name Given subflow:</td>
<td><%=request.getParameter("name") %></td>
</tr> --%>
</table>
<br /> <input type="submit" value="Login" />
</form>
<%-- 
<a href="${flowExecutionUrl}&_eventId=createNewAccountRequested">Create New Account</a> --%>
</body>
</html>


This is display_login_error.jsp JSP file and it is used display the output for the application.



<html>
<body>
Your login credentials are incorrect.
<br/>
<br/>
<a href="./loginFlow">Go back to login page</a>
</body>
</html>


This is display_login_success.jsp JSP file and it is used display the output for the application.



<%@page import="com.cv.spring.webflow.*" %>
<html>
<body>
You have successfully logged in!!!
<br/>
<br/>
<% %> 
<a href="./loginFlow">Go back to login page</a>
</body>
</html>



This is InvalidCredentialsException.java file having the source code to execute business logic.


 

    
package com.cv.spring.webflow;
/**
 @author Chandra Vardhan
 */
public class InvalidCredentialsException extends Exception{

  @Override
  public String getMessage() {
    return super.getMessage();
  }

  @Override
  public synchronized Throwable getCause() {
    return super.getCause();
  }

  @Override
  public StackTraceElement[] getStackTrace() {
    return super.getStackTrace();
  
 
}


This is LoginService.java file having the service/business logic to call the DAO layer and get the information from database.


 

    
package com.cv.spring.webflow;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 */
public class LoginService {
  
  private final static Logger LOGGER = Logger.getLogger(LoginService.class);
  
  public String performLogin(User userthrows InvalidCredentialsException {
    LOGGER.info("In LoginService.performLogin(-) method !!!");
    if (user != null) {
      String loginName = user.getLoginName();
      String password = user.getPassword();
      LOGGER.info("Entered credentials are, username = "+loginName + "and password = "+password);
      if (loginName != null
          && loginName.trim().equalsIgnoreCase("Chandra")
          && password != null
          && password.trim().equalsIgnoreCase("password")) {
        LOGGER.info("User successfully logged in, Credentials are correct!!!");
        // user successfully logged in
        return "success";
      else {
        LOGGER.info("User failed logged in, Credentials are wrong!!!");
        LOGGER.info("Enter credentials as, username = Chandra and password = password");
        throw new InvalidCredentialsException();
      }
    else {
      throw new InvalidCredentialsException();
    }
  }


}


This is User.java file having the source code to execute business logic.


 

    
package com.cv.spring.webflow;

import java.io.Serializable;
/**
 @author Chandra Vardhan
 */
public class User implements Serializable {

  private String loginName;
  private String password;

  public String getLoginName() {
    return loginName;
  }

  public void setLoginName(String loginName) {
    this.loginName = loginName;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

}




This is pom.xml file having the entries of dependency jars and information to build the application .


	
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">.0" encoding="UTF-8"?> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.spring.annotation</groupId> <artifactId>Spring4MVCWebFlowBasic2</artifactId> <name>Spring4MVCWebFlowBasic2</name> <packaging>war</packaging> <version>1.0</version> <properties> <java-version>1.8</java-version> <log4j-version>1.2.16</log4j-version> <org.springframework-version>4.2.0.RELEASE</org.springframework-version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.springframework.webflow</groupId> <artifactId>spring-webflow</artifactId> <version>2.4.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${org.springframework-version}</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <warSourceDirectory>src/main/webapp</warSourceDirectory> <warName>Spring4MVCWebFlowBasic2</warName> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>


This is login-flow.xml file having the spring configuration properties.


 
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.4.xsd">

<var name="user" class="com.cv.spring.webflow.User" />
<!-- Display the login page -->
<view-state id="displayLoginView" view="/WEB-INF/views/display_login.jsp"
model="user">
<transition on="loginCredentialsEntered" to="performLoginAction" />
</view-state>
<action-state id="performLoginAction">
<evaluate expression="loginService.performLogin(user)" />
<transition to="displayLoginSuccessView" />
<transition on-exception="com.cv.spring.webflow.InvalidCredentialsException"
to="displayLoginErrorView" />
</action-state>
<view-state id="displayLoginSuccessView" view="/WEB-INF/views/display_login_success.jsp" />
<view-state id="displayLoginErrorView" view="/WEB-INF/views/display_login_error.jsp" />
</flow>


This is login-servlet-config.xml file having the spring configuration properties.


 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.cv.spring.webflow" />
<!--Define FlowHandlerMapping to tell DispatcherServlet (in web.xml) to
send flow requests to Spring Web Flow -->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="flowRegistry" ref="loginFlowRegistry" />
</bean>
<!-- Define FlowHandlerAdapter to handle Spring Web Flow request call. This
is the Controller class in Spring Web Flow -->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
<property name="flowExecutor" ref="loginFlowExecutor" />
</bean>
</beans>


This is login-webflow-config.xml file having the spring configuration properties.


 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:flow="http://www.springframework.org/schema/webflow-config"
xsi:schemaLocation="http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<flow:flow-executor id="loginFlowExecutor"
flow-registry="loginFlowRegistry" />
<!-- Define the registry that holds references to all the flow related XML
configuration -->
<flow:flow-registry id="loginFlowRegistry">
<flow:flow-location id="loginFlow"
path="/WEB-INF/flows/login-flow.xml" />
</flow:flow-registry>
</beans>


This is spring-config.xml file having the spring configuration properties.


 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="loginService" class="com.cv.spring.webflow.LoginService" />
</beans>



This is web.xml deployment descriptor file and it describes how the web application should be deployed.


	
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Spring Web Flow</display-name>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/flows/login-servlet-config.xml
/WEB-INF/flows/login-webflow-config.xml
/WEB-INF/flows/spring-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>



This is log4j.properties file having the entries for logging the information into the console/file.



#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


No comments:

Post a Comment