Action redirect is not working with list attribute after login in Struts 2

2.4k views Asked by At

I have the following usecase that on Login a CustomerForm should be shown and a ListofCustomers which is retrieved from Database should be shown.

I have written the following code in Struts 2 but my getCustomerList() in CustomerAction is not getting called which is redirected during the Login action in struts.xml

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Struts 2 Entry Point is Filter -->
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Struts2 Application</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>Login.jsp</welcome-file>
    </welcome-file-list>
</web-app> 

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" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />
    <package name="default" extends="struts-default" namespace="/">
        <interceptors>
            <interceptor name="mylogging"
                class="com.rahul.interceptor.MyLoggingInterceptor">
            </interceptor>
            <interceptor-stack name="loggingStack">
                <interceptor-ref name="mylogging" />
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>
        <action name="login" class="com.rahul.action.LoginAction">
        <interceptor-ref name="loggingStack" />
            <result type="redirect">/customer?method=getCustomersList</result>
            <result name="success">Welcome.jsp</result>
            <result name="error">Login.jsp</result>
        </action>
        <action name="customer" class="com.rahul.action.CustomerAction">
        
        <interceptor-ref name="loggingStack" />
            <result name="success">SuccessCustomer.jsp</result>
            <result name="input">Customer.jsp</result>
        </action>
        
    </package>
</struts>

login.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 2 - Login Application</title>
</head>
<body>
    <h2>Struts 2 Login Application</h2>
    <s:actionerror />
    <s:form action="login" method="post">
        <s:textfield name="username" key="label.username" size="20" />
        <s:password name="password" key="label.password" size="20" />
        <s:submit method="execute" key="label.login" align="center" />
    </s:form>
</body>
</html>

loginAction.java:

package com.rahul.action;

public class LoginAction {
    private String userName;
    private String password;

    public String execute() {
        if (this.userName.equals("admin") && this.password.equals("admin123")) {
            return "success";
        } else {
            return "error";
        }
    }

    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;
    }

}

CustomerAction.java:

package com.rahul.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction extends ActionSupport {
    private String name;
    private Integer age;
    private String email;
    private String telephone;
    private List<String> customerList;
    private String countryName;

    public String getCountryName() {
        
        System.out.println("getCountryName method");
        countryName="India";
        
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public String addCustomer() {
        return SUCCESS;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public List<String> getCustomerList() {
        return customerList;
    }

    public void setCustomerList(List<String> customerList) {
        this.customerList = customerList;
    }
    
    
    public String getCustomersList() {
        System.out.println("Inside Customer List");
        customerList = new ArrayList<String>();
        customerList.add("Rahul");
        customerList.add("Saurabh");
        return SUCCESS;
    }
}

welcome.jsp:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome</title>
</head>
<body>
    <h2>
        Howdy,
        <s:property value="username" />
        ...!
    </h2>
    <%@include file="Customer.jsp"%>
</body>
</html>

Customer.jsp:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ 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>Customer Page</title>
</head>
<body>
    <s:form action="customer.action" method="post" validate="true">
        <s:textfield name="name" key="name" size="20" />
        <s:textfield name="age" key="age" size="20" />
        <s:textfield name="email" key="email" size="20" />
        <s:textfield name="telephone" key="telephone" size="20" />
        <s:submit method="addCustomer" key="label.add.customer" align="center" />

    </s:form>
<s:property value="customerList" />


</body>
</html>
1

There are 1 answers

0
Roman C On

You can't pass a method as parameter name. This name is reserved for special parameter used by DMI and have a special syntax. First, if you want to use this parameter and DMI you should enable it.

<constant name="struts.enable.DynamicMethodInvocation" value="true" />

then

<result type="redirect">/customer?method:getCustomersList</result>

Note, use a colon to specify a method name in parameter.

or use equivalent syntax

<result type="redirect">/customer!getCustomersList</result>

You might also wonder: Is a colon safe for friendly-URL use?

Also, don't use .action extension in the struts tags

<s:form namespace="/" action="customer" method="post" validate="true">
    <s:textfield name="name" key="name" size="20" />
    <s:textfield name="age" key="age" size="20" />
    <s:textfield name="email" key="email" size="20" />
    <s:textfield name="telephone" key="telephone" size="20" />
    <s:submit method="addCustomer" key="label.add.customer" align="center" />
</s:form>

Note, the method attribute used by the struts tag to add a special parameter to the form url: method:addCustomer. This parameter only works with DMI. If you used namespace to the action configuration you should use it in the form tag as well.