Showing posts with label Struts 2. Show all posts
Showing posts with label Struts 2. Show all posts

Monday, September 18, 2017

Strut 2 and Spring Integration

To integrate Struts 2 and Spring, get and include the “struts2-spring-plugin-xxx.jar” library into your project classpath.

web.xml for the Struts-Spring integration as follows:

<?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"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">
       
   <display-name>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>

   <listener>
      <listener-class>
         org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>

   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>

</web-app>

The important thing to note here is the listener that we have configured. The ContextLoaderListener is required to load the spring context file. Spring's configuration file is called applicationContext.xml file and it must be placed at the same level as the web.xml file
  
struts.xml

<struts>
<include file="/application/party/configfiles/login.xml"/>
   <package name="default" extends="struts-default-btsl">
   <default-interceptor-ref name="defaultStack" />
   </package>
</struts>

login.xml

<struts>

<package name="login" namespace="/login" extends="struts-default-btsl">
    <action name="login_*" method="{1}" class="loginAction">
            <interceptor-ref name="defaultStack">
                        <param name="securitymanager.exclude">Y</param>
            </interceptor-ref>
            <result>/mjsp/login/tangohomepage.jsp</result>
            <result name="input">/mjsp/login/login.jsp</result>
            <result name="error">/mjsp/login/login.jsp</result>
            <result name="logout">/mjsp/login/logout.jsp</result>
            <result name="securityLogout">/defaultErrorPage2.jsp</result>
            <result name="homepage">/mjsp/login/homepage.jsp</result>
    </action>
</package>   
</struts>

applicationContext.xml


<bean id="loginActionFT" class="com.btsl.login.FT.LoginActionFT"
           parent="loginAction" autowire="default" scope="session">
           <property name="channelDAO" ref="channelDAO" />
           <property name="FTDAOI" ref="FTDAOI" />
</bean>

Tuesday, February 16, 2016

Struts 2 including Action, Service class and accessing input parameters(Part-2)

Struts 2 including Action, Service class including accessing input parameters, use of Value Stack.

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>
   <include file="struts-default.xml"/>
   <package name="a" namespace="/tutorials" extends="struts-default">
      <action name="getTutorials" class="com.javaetutorials.action.TutorialAction">
         <result name="success">/success.jsp</result>
         <result name="error">/error.jsp</result>
      </action>
   </package>
</struts>/

TutorialAction.java(Action class)

package com.javaetutorials.action;
import com.javaetutorials.service.TutorialFinderService;
public class TutorialAction {
      /*Interceptors will pick the query parameter and save it by default.Accessing the input parameters*/
      private String language;
      private String bestTutorialSite;

      public String getBestTutorialSite() {
            return bestTutorialSite;
      }

      public void setBestTutorialSite(String bestTutorialSite) {
            this.bestTutorialSite = bestTutorialSite;
      }

      public String getLanguage() {
            return language;
      }

      public void setLanguage(String language) {
            this.language = language;
      }

      public String execute() {
            TutorialFinderService tf = new TutorialFinderService();
            /* bestTutorialSite must be outside the method so that it can be accessed in JSP using struts-tag lib */
            setBestTutorialSite(tf.getBestTutorialSite(getLanguage()));
            return "success";
      }
}

TutorialFinder.java (Service Class)

package com.javaetutorials.service;

public class TutorialFinderService {
      public String getBestTutorialSite(String language) {
            if(language.equals("java"))
                  return "javaetutorials";
            else
                  return "Language not supported";
      }
}*

Success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix ="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts Example</title>
</head>
<body>
Business Service Executed...
<br>
<s:property value="bestTutorialSite"/> <br>
<s:property value="language"/>
</body>
</html>

Outputs :



Tuesday, February 9, 2016

Struts 2 Architecture (Part-1)

Struts 2 Login example with only 1 action (Part-1)


Struts2 core components are:
1.     Action Classes
2.     Interceptors
3.     Result Pages, JSP of FreeMarker templates

4.     ValueStack, OGNL and Tag Libraries
Struts 2 Architecture Diagram, struts2 interview questions, struts interview questions

Web.xml

Request ----> web.xml (Filter Dispatcher) ----> struts.xml ----> ActionProxy(Action class)
Filters acts as Interceptor (request/response parameters) and required to send request from web.xml to struts.xml

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:form action="verify">
<s:textfield name="uname" label="Enter Username" /><br>
<s:password name="password" label="Enter Password" /><br>
<s:submit value="Click" align="center" />
</s:form>
</body>
<html>




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>
   <include file="struts-default.xml"/>
   <package name="a" namespace="/tutorials" extends="struts-default">
      <action name="getTutorials" class="com.javaetutorials.action.TutorialAction">
         <result name="success">/success.jsp</result>
         <result name="error">/error.jsp</result>
      </action>
   </package>
</struts>

TutorialAction.java (Action Class) --->struts.xml ---> success.jsp/error.jsp

public class TutorialAction {
      public String execute() {
            System.out.println("Entry in execute method");
            return "success";
      }
}

Success.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
SUCCESS PAGE

error.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
Login failed...!!

tutorials acts as namespace



SUCCESS PAGE