Struts 2单选按钮列表

时间:2010-12-14 07:21:23

标签: java jsp struts2 ognl valuestack

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

  

org.apache.jasper.JasperException:tag   '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>

的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>

1 个答案:

答案 0 :(得分:0)

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

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