Java spring框架:应用程序上下文不设置属性

时间:2013-12-05 16:15:51

标签: java html spring inheritance

问题:

当我在chrome中运行时,内容div不会显示图像字符串(在接收对象的应用程序上下文中设置)。

ItemGroup类有一个Items列表 和Receipt是抽象类Item

的子类

这是我在netbeans中的代码:

的applicationContext.xml:

 <bean id="receipt1" class="domain.receipts.Receipt" >
        <property name="id" value="1"/>
        <property name="image" value="Images/receipt.png"/>
 </bean>

 <bean id="receipts" class="domain.ItemGroup">
        <property name="items">
            <list>
                <ref bean="receipt1"/>        
            </list>
        </property>
        <property name="image" value="/Images/klantenkaart.png"/>
 </bean>

HomeController中:

@Controller
public class HomeController 
{
private ItemGroupService itemGroupService;

@Autowired
private ItemGroup receipts;
@Autowired
private ItemGroup shoppinglists;
@Autowired
private ItemGroup cards;

@Autowired
public HomeController(ItemGroupService ItemGroupServiceImpl)
{
this.itemGroupService=ItemGroupServiceImpl;
}


public List<Item> getReceipts()
{
    return receipts.getItems();
}

@RequestMapping(value = {"/index"},method = RequestMethod.GET)
public String showHomePage(Model model) 
{
model.addAttribute("itemGroup", new ItemGroup());
return "index";
}

@RequestMapping(value = {"/index"},method = RequestMethod.POST)
public String onSubmit(@ModelAttribute("itemGroup") ItemGroup itemGroup, Model model)
{

    if(itemGroup.getName().equals("receipts"))
    {
       model.addAttribute("itemList",receipts.getItems());
    }
    else if(itemGroup.getName().equals("cards"))
    {
       model.addAttribute("itemList",cards.getItems());
    }
    return "index";
    }
 }

的index.jsp

<div id="header">
  <table>
      <tr>
   <form:form method="POST" action="index.htm" modelAttribute="itemGroup">
     <form:input path="name" type="hidden" value="receipts" /> 
     <input type="image" src="Images/receipt.png" height="150px" width="180px" alt="Submit" value="receipts">    
   </form:form>
   <form:form method="POST" action="index.htm" modelAttribute="itemGroup">     
     <form:input path="name" type="hidden" value="shoppinglists" /> 
     <input type="image" src="Images/shoppinglist.png" height="150px" width="180px" alt="Submit" value="shoppinglists">
   </form:form> 
   <form:form method="POST" action="index.htm" modelAttribute="itemGroup">     
     <form:input path="name" type="hidden" value="cards" /> 
     <input type="image" src="Images/loyalitycard.png" height="150px" width="180px" alt="Submit" value="cards">
  </form:form> 
   <form:form method="POST" action="index.htm" modelAttribute="itemGroup">     
     <form:input path="name" type="hidden" value="vouchers" /> 
     <input type="image" src="Images/voucher.png" height="150px" width="180px" alt="Submit" value="vouchers">
  </form:form> 
     </tr>
  </table>

</div>

<div id="leftcol">
    </br>
</div>

<div id="content">
      </br>
      <h1> ${itemList}</h1>     
           <c:forEach items="${itemList}" var="prod" >
                <h1>${prod.image}<h1>            
           </c:forEach>   
</div>

项目类:

public abstract class Item 
{
    private int id;
    private String barcode;
    private Date creationDate;
    protected String image;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBarcode() {
        return barcode;
    }

    public void setBarcode(String barcode) {
        this.barcode = barcode;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

1 个答案:

答案 0 :(得分:0)

 model.addAttribute("itemList",cards.getItems());

调试这篇文章可能会找到一些东西!