Struts2 带有 s:action 标签

发布于 2024-08-29 08:39:52 字数 3523 浏览 3 评论 0原文

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

  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.javawelcome.jsp

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

} 

<%@ 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 识别为集合、数组、列表或迭代器 我应该指定什么来在文件welcome.jsp 中的s:select 标记中列出属性

代码有什么问题请详细告诉我。如果您可以将更正后的版本发送给我。为什么 标签不起作用?

这是我收到的错误

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

Here is a small test application that does following things

  1. ask user to enter his name and submit - (index.jsp)
  2. as a result of index.jsp is the welcome.jsp page that asks user to select his/her blood group

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 is unable to identify bglist as a collection or Array or List or iterator. WHAT SHOULD I ASSIGN TO list ATTRIBUTE IN THE s:select TAG IN THE FILE welcome.jsp

What is wrong with the code please tell me in detail. If you could send me the corrected version. WHY IS THE <s:action> tag not working ?

This is the error i am getting

Apr 13, 2010 1:49:19 PM
org.apache.catalina.core.ApplicationDispatcher
invoke SEVERE: Servlet.service() for
servlet jsp threw exception tag
'select', field 'list': The requested
list key 'bglist' could not be
resolved as a
collection/array/map/enumeration/iterator
type. Example: people or people.{name}
- [unknown location]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萤火眠眠 2024-09-05 08:39:52

看来您误解了基本的 struts2 流程。

页面welcome.jsp 是操作“MyName”(坏名字,顺便说一句)的结果页面(视图)。这意味着,当生成welcome.jsp页面时,操作“MyName”(类module1.User)刚刚被“执行”,并且该对象(类module1.User的一个实例)就是位于显示结果时显示“范围”(值堆栈)。
因此,welcome.jsp 正在 module1.User 类中查找“bglist”列表。

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

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

It seems you are misunderstanding the basic struts2 flow.

The page welcome.jsp is the result page (view) for the action "MyName" (bad name, BTW). It means, when the welcome.jsp page is being generated, the action "MyName" (class module1.User) has just been "executed", and it's that object (an instance of class module1.User) the one which is in the present "scope" (the valu stack) when the result is shown.
So, the welcome.jsp is looking for the "bglist" list in the module1.User class.

You need to rethink you action mappings.

(Your confusion can be related to your statement "as a result of index.jsp is the welcome.jsp page" ... you must think jsp pages as result of ACTIONS, not of other jsps)

像极了他 2024-09-05 08:39:52

bglist 是在 Action SelectBloodGroup 中声明和定义的,它永远不会进入上下文。
您的应用程序的流程是 Index.jsp(submit) --> module1.User(操作) --> welcome.jsp,因此 bglist 从未被实例化,您的类 SelectBloodGroup 也从未被调用。

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

要在 jsp 选择列表中提供硬编码值,您可以使用以下代码:

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

声明变量时请尝试使用驼峰命名法。
希望这有帮助。

bglist is declared and defined in Action SelectBloodGroup which never comes into context.
The flow of your application is Index.jsp(submit) --> module1.User(Action) --> welcome.jsp, so bglist never got instantiated nor your class SelectBloodGroup got called.

If you want to use initialize your bglist in Action then place it in module1.User or you can also give values of list in jsp if these values are going to be hard coded(it seems to be) or for best practice you can use resource bundles for static values(more better than hard coding).

To give hard coded values in jsp Select List you can use the below code:

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

Please try to use camelCase pattern while declaring variables.
Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文