Struts 2 Action重定向不适用于以下场景

时间:2014-01-24 07:27:14

标签: java jsp configuration struts2 dmi

我有以下用例:Login应显示CustomerForm,并显示从数据库检索到的ListofCustomers。 我已经在Struts 2中编写了以下代码但getCustomerList中的CustomerAction未被调用,而Login行动期间struts.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> 被重定向

1。)web.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>

2.Struts.xml

<%@ 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>

3.)的Login.jsp

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

}

4.)LoginAction.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;
    }
}

5.CustomerAction.java

<%@ 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>

6.)的welcome.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>

7.)Customer.jsp

{{1}}

1 个答案:

答案 0 :(得分:0)

您无法传递method作为参数名称。此名称保留给DMI使用的特殊参数,并具有特殊语法。首先,如果要使用此参数和DMI,则应启用它。

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

然后

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

注意,使用冒号在参数中指定方法名称。

或使用等效语法

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

您可能也想知道:Is a colon safe for friendly-URL use?

另外,请勿在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>

注意,struts标记使用的方法属性将特殊参数添加到表单url:method:addCustomer。此参数仅适用于DMI。如果您对操作配置使用了命名空间,那么您也应该在form标记中使用它。