简体   繁体   English

在 Struts 2 中登录后,动作重定向不适用于列表属性

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

I have the following usecase that on Login a CustomerForm should be shown and a ListofCustomers which is retrieved from Database should be shown.我有以下用例,在Login时应显示CustomerForm应显示从数据库中检索的ListofCustomers

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我已经在 Struts 2 中编写了以下代码,但是我在CustomerAction getCustomerList()没有被调用,它在struts.xml中的Login操作期间被重定向

web.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 : 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 : 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 : 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 : 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 : 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 : 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>

You can't pass a method as parameter name.您不能将method作为参数名称传递。 This name is reserved for special parameter used by DMI and have a special syntax.此名称保留用于DMI使用的特殊参数,并具有特殊语法。 First, if you want to use this parameter and DMI you should enable it.首先,如果您想使用此参数和 DMI,您应该启用它。

<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?您可能还想知道:对于友好的 URL 使用冒号是否安全?

Also, don't use .action extension in the struts tags另外,不要在 struts 标签中使用.action扩展名

<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 .注意,struts 标签使用的 method 属性向表单 url 添加了一个特殊参数: method:addCustomer This parameter only works with DMI.此参数仅适用于 DMI。 If you used namespace to the action configuration you should use it in the form tag as well.如果你在动作配置中使用了命名空间,你也应该在form标签中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM