2014-01-24 93 views
1

我有以下用例Login上應該顯示一個CustomerForm,並且應該顯示從數據庫檢索到的ListofCustomers。 我寫了下面的代碼中的Struts 2,但我在CustomerActiongetCustomerList是沒有得到所謂的這是在struts.xmlStruts 2操作重定向不適用於下面的場景

1. Login行動期間重定向)的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> 

2。 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> 

3)Login.j屬

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

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

} 

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

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

7)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> 
+2

你沒有接受你以前的帖子的答案,你正在發佈另一個。你認爲花時間回答問題很容易嗎?如何在stackoverflow.com上完成此操作,如果答案解決了您的問題,請點擊灰色的勾號圖標接受它。 –

+1

@Uchenna Nwanyanwu它沒有解決我的問題,這就是爲什麼我想提交我寫的代碼。 –

+1

如果它沒有解決您的問題,爲什麼您將帖子的標題更改爲「Struts 2 Action redirect now working」。你應該用代碼更新你以前的問題,而不是創建一個新的問題。 –

回答

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標記中使用它。