Spring:不会调用ModelAttribute

时间:2014-07-02 13:55:58

标签: java spring annotations spring-annotations modelattribute

我有一个控制器用于显示项目列表,但我找不到为什么@ModelAttribute方法没有被调用。

这是我的控制者:

@Controller(value = "projectListController")
@RequestMapping(value = "VIEW")
public class ProjectListController {

@Autowired
@Qualifier("projectService")
private ProjectService projectService;

@RenderMapping
public String showProjects() {

    return "projectList";
}

@ModelAttribute("projectListCount")
public Long getProjectListCount() {

    return projectService.getProjectListCount();
}

@ModelAttribute("projectFilter")
public ProjectFilter getProjectFilter(
        @RequestParam(value = "cur", required = false) String curString,
        @RequestParam(value = "delta", required = false) String deltaString,
        @RequestParam(value = "orderByCol", required = false) String orderByCol,
        @RequestParam(value = "orderByType", required = false) String orderByType) {

    return new ProjectFilter(curString, deltaString, orderByCol, orderByType);
}

@ModelAttribute("projectList")
public List<TPrProgetti> getProjectList(
        @ModelAttribute("projectFilter") ProjectFilter projectFilter) {

    return projectService.getProgettiList(projectFilter.getCur(), projectFilter.getDelta(),
            projectFilter.getOrderByCol(), projectFilter.isAsc());
}
}

调用该方法的顺序是:

  1. 模型projectListCount
  2. 模型projectList
  3. 渲染projectList
  4. 我不明白为什么Model projectFilter没有被调用,被用作render方法中的参数。 我看到Spring更喜欢调用ProjectFilter的构造函数并通过setter设置其字段。如何完成这种映射是另一回事......

    修改

    为了完成并回答评论,这个项目是一个部署在Liferay中的portlet,它使用Spring。

    jsp是这样的:

    <liferay-ui:search-container emptyResultsMessage="No project found"
        orderByCol="${projectFilter.orderByCol}" orderByType="${projectFilter.orderByType}"
        delta="${projectFilter.delta}">
        <liferay-ui:search-container-results total="${progettiCount}"
            results="${projectList}">
        </liferay-ui:search-container-results>
    
        <liferay-ui:search-container-row
            className="it.eng.liferay.portlet.domain.TPrProgetti" keyProperty="id" modelVar="project">
            <liferay-ui:search-container-column-text name="Codice" property="codProg"  orderable="<%= true %>" orderableProperty="codProg"/>
            <liferay-ui:search-container-column-text name="Descrizione"  property="descrizione" orderable="<%= true %>" orderableProperty="descrizione"/>
            </liferay-ui:search-container-row>
        <liferay-ui:search-iterator />
    </liferay-ui:search-container>
    

1 个答案:

答案 0 :(得分:0)

我刚刚使用了一个包含每个ModelAttribute的ModelAttribute方法,如下所示:

@ModelAttribute
public void populateModel(Model model,
        @RequestParam(value = "cur", required = false) String curString,
        @RequestParam(value = "delta", required = false) String deltaString,
        @RequestParam(value = "orderByCol", required = false) String orderByCol,
        @RequestParam(value = "orderByType", required = false) String orderByType) {

    model.addAttribute("progettiCount", projectService.getProgettiCount());
    model.addAttribute("projectFilter", projectFilter);
    model.addAttribute("projectList", projectService.getProgettiList(projectFilter.getCur(), projectFilter.getDelta(),
                    projectFilter.getOrderByCol(), projectFilter.isAsc()));
}

我不太喜欢它,但它确实有用。

我还尝试使用最新版本的4.0版4.0,但问题没有改变。