Struts 2 单选按钮列表

发布于 2024-10-07 04:33:43 字数 3998 浏览 6 评论 0原文

我正在研究 Struts 2 单选按钮。 我想从我的操作类中检索列表,但它给出以下错误

org.apache.jasper.JasperException:标签 '广播',字段'列表',名称 'user.yourGender':请求的列表 键“#user.gender”不能 解决为 集合/数组/映射/枚举/迭代器 类型。示例:人或人。{name} - [未知位置]

我的动作课和用户类如下

HelloAction

package com.geekcap.struts2.action;

import com.geekcap.struts2.model.User;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.ArrayList;

public class HelloAction extends ActionSupport
{
 private User user;  

 public String execute() throws Exception
 {    
  return "success";
 }

 public void validate()
 {
  if(user.getName().length()==0)
  {
   addFieldError("user.name", "User Name is required");
  }

  if(user.getAge()==0)
  {
   addFieldError("user.age","Age is required");
  }

  if(user.getPassword().length()==0)
  {
   addFieldError("user.password","Please enter your password !");
  }

 /* if(user.getGender().equals("-1"))
  {
   addFieldError("user.gender","Please select gender !");
  }*/
 }



 public User getUser()
 {
  return user;
 }

 public void setUser(User userbean)
 {
  user=userbean;
 }

}

用户类

package com.geekcap.struts2.model;

import java.util.List;
import java.util.ArrayList;

public class User
{
 private String name,password;
// private List like;
 private int age;
 private List<String> gender;
 private String yourGender;

 public User()
 {
  gender= new ArrayList<String>();
  gender.add("MALE");
  gender.add("FEMALE");
  gender.add("UNKNOWN");
 }


 public String getName() 
 {
  return name;
 }

 public void setName(String name) 
 {
  this.name = name;
 }

 public String getPassword()
 {
  return password;
 }

 public void setPassword(String password)
 {
  this.password=password;
 }

 public int getAge()
 {
  return age;
 }

 public void setAge(int age)
 {
  this.age=age;
 }

 public List<String> getGender()
 {
  return gender;
 }

 public void setGender(List<String> gender)
 {
  this.gender=gender;
 }

 public void setYourGender(String yourGender)
 {
  this.yourGender=yourGender;
 }

 public String getYourGender()
 {
  return  yourGender;
 }

 public String getDefaultGenderValue()
 {
  return "UNKNOWN";
 }

helloForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="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=UTF-8">
<title>Welcome to Struts 2</title>
</head>
<body>

<s:form action="Hello"> 
  <s:textfield name="user.name" label="User name" value="shahid"/>
  <s:password name="user.password" label="Password"/>
  <s:textfield name="user.age" label="Age"/>    
  <s:radio label="Gender" name="user.yourGender" list="user.gender" value="defaultGenderValue"/>     
  <s:submit/>
</s:form>

</body>
</html>

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="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=UTF-8">
<title>Hello, Struts 2</title>
</head>
<body>
<h4>
Hello, <s:property value="user.name"/>!
<br>Your password :<s:property value="user.password"/></br>
<br>your age :<s:property value="user.age"/></br>
<br>Gender :<s:property value="user.yourGender"/></br>
</h4> 
</body>
</html>

am working on Struts 2 radio button.
I want to retrieve the list from my action class but it is giving following error

org.apache.jasper.JasperException: tag
'radio', field 'list', name
'user.yourGender': The requested list
key '#user.gender' could not be
resolved as a
collection/array/map/enumeration/iterator
type. Example: people or people.{name}
- [unknown location]

my action class & user class is as follow

HelloAction

package com.geekcap.struts2.action;

import com.geekcap.struts2.model.User;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.ArrayList;

public class HelloAction extends ActionSupport
{
 private User user;  

 public String execute() throws Exception
 {    
  return "success";
 }

 public void validate()
 {
  if(user.getName().length()==0)
  {
   addFieldError("user.name", "User Name is required");
  }

  if(user.getAge()==0)
  {
   addFieldError("user.age","Age is required");
  }

  if(user.getPassword().length()==0)
  {
   addFieldError("user.password","Please enter your password !");
  }

 /* if(user.getGender().equals("-1"))
  {
   addFieldError("user.gender","Please select gender !");
  }*/
 }



 public User getUser()
 {
  return user;
 }

 public void setUser(User userbean)
 {
  user=userbean;
 }

}

User class

package com.geekcap.struts2.model;

import java.util.List;
import java.util.ArrayList;

public class User
{
 private String name,password;
// private List like;
 private int age;
 private List<String> gender;
 private String yourGender;

 public User()
 {
  gender= new ArrayList<String>();
  gender.add("MALE");
  gender.add("FEMALE");
  gender.add("UNKNOWN");
 }


 public String getName() 
 {
  return name;
 }

 public void setName(String name) 
 {
  this.name = name;
 }

 public String getPassword()
 {
  return password;
 }

 public void setPassword(String password)
 {
  this.password=password;
 }

 public int getAge()
 {
  return age;
 }

 public void setAge(int age)
 {
  this.age=age;
 }

 public List<String> getGender()
 {
  return gender;
 }

 public void setGender(List<String> gender)
 {
  this.gender=gender;
 }

 public void setYourGender(String yourGender)
 {
  this.yourGender=yourGender;
 }

 public String getYourGender()
 {
  return  yourGender;
 }

 public String getDefaultGenderValue()
 {
  return "UNKNOWN";
 }

helloForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="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=UTF-8">
<title>Welcome to Struts 2</title>
</head>
<body>

<s:form action="Hello"> 
  <s:textfield name="user.name" label="User name" value="shahid"/>
  <s:password name="user.password" label="Password"/>
  <s:textfield name="user.age" label="Age"/>    
  <s:radio label="Gender" name="user.yourGender" list="user.gender" value="defaultGenderValue"/>     
  <s:submit/>
</s:form>

</body>
</html>

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="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=UTF-8">
<title>Hello, Struts 2</title>
</head>
<body>
<h4>
Hello, <s:property value="user.name"/>!
<br>Your password :<s:property value="user.password"/></br>
<br>your age :<s:property value="user.age"/></br>
<br>Gender :<s:property value="user.yourGender"/></br>
</h4> 
</body>
</html>

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

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

发布评论

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

评论(1

南城追梦 2024-10-14 04:33:43

您确定 JSP 中的列表 gender 不是 null 吗?

如果它为 null,那么 struts 将看不到它,因此认为它不存在

Are you sure the list gender is NOT null in the JSP?

If it is null, then struts will not see it and therefore think it isn't there

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