0

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

org.apache.jasper.JasperException:标签'radio',字段'list',名称'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>

你好.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>
4

1 回答 1

0

您确定该列表gender不在nullJSP 中吗?

如果它为空,则 struts 将看不到它,因此认为它不存在

于 2010-12-14T07:52:25.930 回答