2013-02-25 46 views
5

Tôi nhận được lỗi ở trên.javax.el.PropertyNotFoundException: Tên 'thuộc tính' không tìm thấy trên loại java.lang.String

Những gì tôi đang cố gắng làm là viết một ứng dụng có số lượng tên không xác định, sau đó in chúng ra trên một trang mới. Điều này được cho là cho một ứng dụng bowling, nhưng bây giờ tôi chỉ muốn có được một danh sách các tên. Ý tưởng là cho mỗi tên để đi vào một đối tượng người chơi, sau đó lần lượt được lưu trữ trong danh sách các cầu thủ. Nếu bất cứ ai có thể giúp đỡ, tôi sẽ đánh giá cao nó.

Đây là mã điều khiển của tôi:

package multiplayergame; 

import java.util.ArrayList; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.SessionAttributes; 
import org.springframework.web.servlet.ModelAndView; 


@Controller 
@SessionAttributes 
public class MultiplayerController { 

int score; 
int roll; 
Game game = new Game(); 
GameProperties gameProps = new GameProperties(); 
int playerNo = 0; 

ArrayList<PlayerGame> players = new ArrayList<>(); 
PlayerGame player; 

@RequestMapping(value = "/home", method = RequestMethod.GET) 
public ModelAndView home() { 

    return new ModelAndView("home", "command", gameProps);  
} 

@RequestMapping(value = "/nextName", method = { RequestMethod.POST, RequestMethod.GET}) 
public ModelAndView homeNext(MContact mcontact, BindingResult result) {  
    player = new PlayerGame(); 

    player.setName(mcontact.getName()); 
    players.add(player); 
    gameProps.setPlayers(players); 
    playerNo++; 
    return new ModelAndView("home", "command", gameProps);  
} 

@RequestMapping(value = "/test", method = RequestMethod.POST) 
public ModelAndView playNext(GameProperties gameProps2, ModelMap model) { 

    model.addAttribute("players", gameProps.getPlayers()); 
    model.addAttribute("name", gameProps.getPlayers().get(playerNo).getName()); 

    return new ModelAndView("test", "players", gameProps2); 

} 
} 

này chứa thông tin chi tiết cho mỗi người chơi:

package multiplayergame; 

public class PlayerGame { 
private int score; 
private int pins; 
private String name; 
private int roll; 
private int nOfPlayers; 
private int playerNo; 


Game game = new Game(); 

public Game getGame() { 
    return game; 
} 
public void setGame(Game game) { 
    this.game = game; 
} 
public int getScore() { 
    return score; 
} 
public void setScore(int score) { 
    this.score = score; 
} 
public int getPins() { 
    return pins; 
} 
public void setPins(int pins) { 
    this.pins = pins; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public int getRoll() { 
    return roll; 
} 
public void setRoll(int roll) { 
    this.roll = roll; 
} 
public int getnOfPlayers() { 
    return nOfPlayers; 
} 
public void setnOfPlayers(int nOfPlayers) { 
    this.nOfPlayers = nOfPlayers; 
} 
public int getPlayerNo() { 
    return playerNo; 
} 
public void setPlayerNo(int playerNo) { 
    this.playerNo = playerNo; 
} 
} 

Đây là mã cho tất cả các thuộc tính game, chẳng hạn như ghi bàn, vv

package multiplayergame; 

import java.util.ArrayList; 

public class GameProperties { 

private int score; 
private int pins; 
private String name; 
private int roll; 
private int nOfPlayers; 
PlayerGame player; 
private ArrayList<PlayerGame> players; 
private int playerNo; 

public int getScore() { 
    return score; 
} 
public void setScore(int score) { 
    this.score = score; 
} 
public int getPins() { 
    return pins; 
} 
public void setPins(int pins) { 
    this.pins = pins; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public int getRoll() { 
    return roll; 
} 
public void setRoll(int roll) { 
    this.roll = roll; 
} 
public int getnOfPlayers() { 
    return nOfPlayers; 
} 
public void setnOfPlayers(int nOfPlayers) { 
    this.nOfPlayers = nOfPlayers; 
} 
public ArrayList<PlayerGame> getPlayers() { 
    return players; 
} 
public void setPlayers(ArrayList<PlayerGame> players) { 
    this.players = players; 
} 
public int getPlayerNo() { 
    return playerNo; 
} 
public void setPlayerNo(int playerNo) { 
    this.playerNo = playerNo; 
} 
} 

Sau đây là các tệp JSP của tôi, đầu ra là:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 


<html> 
<head> 
<title>Test output</title> 
</head> 
<body> 

<h2>Test Roll</h2> 

<c:forEach var="player" items="players"> 
Name <c:out value="${player.name}"/><p> 
</c:forEach> 

</body> 
</html> 

Đây là trang chủ:

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<head> 
<title>Bowling</title> 
</head> 
<body> 
<h2>Welcome players!</h2> 

<h2>Please enter your names.</h2> 

<form:form method="post" action="/multiplayergame/nextName"> 

<table> 
<tr> 
    <td><form:label path="name">Name</form:label></td> 
    <td><form:input path="name" /></td> 
</tr> 
<tr> 
    <td colspan="2"> 
     <input type="submit" name = "button" value="Next Player"/> 
    </td> 
</tr> 
</table>  
</form:form> 
<form:form method="post" action="/multiplayergame/test"> 
<tr> 
    <td colspan="2"> 
     <input type="submit" name = "button" value="Play"/> 
    </td> 
</tr> 
</form:form> 
</body> 
</html> 

Trả lời

21

Thay đổi này <c:forEach var="player" items="players">-<c:forEach var="player" items="${players}">

Như đề cập "người chơi" của bạn chuỗi. nó đang cố gắng tìm tên thuộc tính trong chuỗi.

Các vấn đề liên quan