【问题标题】:Struts2 with s:action tag带有 s:action 标签的 Struts2
【发布时间】:2010-04-13 08:33:12
【问题描述】:

这是一个执行以下操作的小型测试应用程序

  1. 要求用户输入他的姓名并提交 - (index.jsp)
  2. 由于 index.jsp 是welcome.jsp 页面,要求用户选择他/她的血型

index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>   
<%@ taglib prefix="s" uri="/struts-tags" %>   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
<html>   
  <head>   </head>   
   <body>   
    <form action="MyName">   
    <s:textfield name="UserName" label="Enter Your Name"/>   
    <s:submit/>   
    </form><br>   
  </body>   
</html>    

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>   
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">   
<struts>   
<package name="module1" namespace="" extends="struts-default">   
<action name="MyName" class="module1.User">   
    <result>/Welcome.jsp</result>   
</action>   
<action name="Blood_Group" class="module1.SelectBloodGroup" method="bloodGroupList"/>   
</package>   

</struts>

SelectBloodGroup.java

package module1;   

import java.util.ArrayList;   
import com.opensymphony.xwork2.ActionSupport;   
public class SelectBloodGroup extends ActionSupport{   
    private ArrayList<BloodGroup> bglist;   

    public String bloodGroupList(){   
        bglist = new ArrayList<BloodGroup>();   
        bglist.add(new BloodGroup("1","A+"));   
        bglist.add(new BloodGroup("2","B+"));   
        bglist.add(new BloodGroup("3","AB+"));   
        bglist.add(new BloodGroup("4","O+"));   
        bglist.add(new BloodGroup("5","A-"));   
        bglist.add(new BloodGroup("6","B-"));   
        bglist.add(new BloodGroup("7","AB-"));   
        bglist.add(new BloodGroup("8","O-"));   
        return "SUCCESS";   
    }   

    public ArrayList<BloodGroup> getBglist(){   
        return bglist;   
    }   

}   
class BloodGroup{   
    private String id;   
    private String bg;   

    BloodGroup(String id,String bg){   
        this.id=id;   
        this.bg=bg;   
    }   

} 

welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>   
<%@ taglib prefix="s" uri="/struts-tags" %>   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
<html>   
  <head>   

  </head>   

  <body>   
    <s:action name="Blood_Group" executeResult="false"/>    

    //***************here is the problem***************   
    <s:select list="bglist" listKey="id" listValue="bg"/>   
   //***********************************************   

  </body>   
</html>   

Struts 无法将bglist 识别为集合、数组、列表或迭代器。我应该分配什么来列出 s 中的属性:选择文件中的标签 welcome.jsp

代码有什么问题请详细告诉我。如果你能把更正的版本发给我。为什么&lt;s:action&gt; 标签不起作用?

这是我遇到的错误

2010 年 4 月 13 日下午 1:49:19 org.apache.catalina.core.ApplicationDispatcher 调用 SEVERE: Servlet.service() for servlet jsp 抛出异常标记 'select', field 'list': 请求的 列表键“bglist”不能 解决为 集合/数组/映射/枚举/迭代器 类型。示例:人或人。{name} - [未知位置]

【问题讨论】:

    标签: struts2


    【解决方案1】:

    您似乎误解了基本的 struts2 流程。

    welcome.jsp 页面是操作“MyName”(坏名,顺便说一句)的结果页面(视图)。这意味着,当生成welcome.jsp 页面时,动作“MyName”(类module1.User)刚刚被“执行”,它就是那个对象(类module1.User 的一个实例)在显示结果时显示“范围”(值堆栈)。 所以,welcome.jsp 在 module1.User 类中寻找“bglist”列表。

    您需要重新考虑您的操作映射。

    (您的困惑可能与您的陈述有关“由于 index.jsp 是welcome.jsp 页面”...您必须认为 jsp 页面是 ACTIONS 的结果,而不是其他 jsps 的结果)

    【讨论】:

      【解决方案2】:

      bglist 在动作 SelectBloodGroup 中声明和定义,它永远不会出现在上下文中。 您的应用程序的流程是 Index.jsp(submit) --> module1.User(Action) --> welcome.jsp,所以 bglist 永远不会被实例化,您的类也不会被实例化SelectBloodGroup 被调用。

      如果您想在 Action 中使用初始化 bglist,则将其放在 module1.User 中,或者如果这些值将被硬编码(似乎是)或者为了最佳实践,您也可以在 jsp 中给出列表值可以将资源包用于静态值(比硬编码更好)。

      要在 jsp Select List 中给出硬编码值,您可以使用以下代码:

      <select name="bgList" id="bgListId">
          <option value="1">Bpositive</option>
          <option value="someValue">someLabel</option>
      </select>
      

      请在声明变量时尝试使用驼峰式模式。 希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-23
        • 1970-01-01
        • 2012-06-25
        • 1970-01-01
        • 1970-01-01
        • 2015-09-10
        • 1970-01-01
        相关资源
        最近更新 更多