2011-03-04 88 views
0

我想知道是否可以使用JSP頁面中的標記調用控制器方法,有點像在JSF中完成的。使用SpEL和eval調用控制器方法

我的控制器

@Controller(value="planesController") 
@RequestMapping({"/planes"}) 
public class PlanesController { 

    @Autowired 
    private PlanesDAO planesDAO; 

    public List<Plane> allPlanes(){ 

     return planesDAO.getAll(); 
} 

我的JSP

<sf:form> 
    <s:eval expression="planesController.allPlanes()" var="planes" /> 

    <sf:checkboxes items="${planes}" path="planes" id="avions"/> 

</sf:form> 

我不斷收到異常:

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'planesController' cannot be found on null 

我知道我可以使用model.addAttribute,但是我從幾個JSP頁面調用這個方法,我認爲其中的一個標記允許從視圖訪問bean。

我使用的是春天提前

+0

答案在[forum]上提供(http://forum.springsource.org/showthread.php?104736-Using-SpEL-and-eval-to-call-a-controller-method):使用' s:eval expression =「@ planesController.allPlanes()」var =「planes」/>'。 – 2012-05-03 09:40:24

回答

0

讓視圖呼叫控制器代碼3.0.5

由於是完全錯誤的,如果不管它工作與否。這是MVC倒退。

如果你寫一個服務來做你想做的事情並讓你的視圖訪問那個服務,但是你仍然違反了MVC,那會更好。在您的控制器中首先完成所有這些調用會更好,並將結果模型傳遞到視圖中,而無需任何服務調用。

+0

Humm我看到了,我猜想一個壞習慣......謝謝 – ufasoli 2011-03-04 21:32:38