将modelAttribute从jsp绑定到控制器

时间:2015-07-30 09:48:45

标签: java jsp spring-mvc

这是我的Controller类:

@Controller
@SessionAttributes({"list", "selectedCompany"})
public class ProspectController {

protected static Logger logger = Logger.getLogger("controller");

@Resource(name = "ProspectWebServiceTemplate")
private WebServiceTemplate prospectWebServiceTemplate;
@Resource(name = CountryWebServiceTemplate")
private WebServiceTemplate countryWebServiceTemplate;
@Resource(name = "CompanyWebServiceTemplate")
private WebServiceTemplate companyWebServiceTemplate;

/**
 * Handles and retrieves the subscribe page
 */
@RequestMapping(value = "/addProspect", method = RequestMethod.GET)
public String getAddProspectPage(@ModelAttribute("prospectAttribute") ProspectDto prospectDto,Model model) {
    logger.debug("Received request to show add prospect page");

    final GetAllCountriesRequest request = new GetAllCountriesRequest();
    GetAllCountriesResponse response = (GetAllCountriesResponse) countryWebServiceTemplate
            .marshalSendAndReceive(request);


    List<CountryDto> list = new ArrayList<CountryDto>();
    List<CountryDto> allCountries =  response.getCountriesDto();
    if (response != null && allCountries != null) {
        for (CountryDto dto : allCountries) {
            list.add(dto);
        }
    }

    model.addAttribute("list", list);

    final GetAllCompaniesRequest requestComp = new GetAllCompaniesRequest();
    GetAllCompaniesResponse responseComp = (GetAllCompaniesResponse) companyWebServiceTemplate
            .marshalSendAndReceive(requestComp);

    List<CompanyDto> listComp = new ArrayList<CompanyDto>();
    List<CompanyDto> allCompanies =  responseComp.getCompaniesDto();
    if (responseComp != null &&  allCompanies != null) {
        for (CompanyDto dto : allCompanies) {
            listComp.add(dto);
        }
    }
    model.addAttribute("listComp", listComp);



    model.addAttribute("prospectAttribute", new SaveProspectRequest());
    return "addProspect";
}


@RequestMapping(value = "/saveProspect", method = RequestMethod.POST)
public String doSave(
        @ModelAttribute("prospectAttribute")  SaveProspectRequest request,
        Model model) {
    logger.debug("Received request to subscribe");
    try {
        SaveProspectResponse response = (SaveProspectResponse) prospectWebServiceTemplate
                .marshalSendAndReceive(request);
        logger.debug(response.getId());

        model.addAttribute("response", "Success! ");
        model.addAttribute("prospect", response.getFirstName());

    } catch (SoapFaultClientException sfce) {
        logger.error("We sent an invalid message", sfce);
        model.addAttribute("error",
                "Validation error! We cannot process your subscription");

    } catch (Exception e) {
        logger.error("Unexpected exception", e);
        model.addAttribute("error",
                "Server error! A unexpected error has occured");
    }

    model.addAttribute("prospectAttribute", request);

    return "addProspect";
}

}

这是我的jsp文件

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
    <!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=ISO-8859-1">
  <title>Prospect</title>
  </head>

 <body>

<c:url var="addUrl" value="/example/saveProspect" />
<form:form modelAttribute="prospectAttribute" method="POST"
    action="${addUrl}">
    <table>
        <tr>
            <td><form:label path="prospectDto.firstName">First Name:</form:label></td>
            <td><form:input path="prospectDto.firstName" /></td>
        </tr>
        <tr>
            <td><form:label path="prospectDto.lastName">Last Name:</form:label></td>
            <td><form:input path="prospectDto.lastName" /></td>
        </tr>
        <tr>
            <td><form:label path="prospectDto.function">Function:</form:label></td>
            <td><form:input path="prospectDto.function" /></td>
        </tr>
        <tr>
            <td><form:label path="prospectDto.email">Email:</form:label></td>
            <td><form:input path="prospectDto.email" /></td>
        </tr>
        <tr>
            <td><form:label path="prospectDto.telephone">Telephone:</form:label></td>
            <td><form:input path="prospectDto.telephone" /></td>
        </tr>
        <tr>
            <td><form:label path="prospectDto.typeLead">Type Lead:</form:label></td>
            <td><form:select path="prospectDto.typeLead">
                    <form:option value="">-- Select Type Lead --</form:option>
                    <form:option value="LEAD1">LEAD1</form:option>
                    <form:option value="LEAD2">LEAD2</form:option>
                    <form:option value="LEAD3">LEAD3</form:option>
                    <form:option value="CLIENT">CLIENT</form:option>
                    <form:option value="REFUSE">REFUSE</form:option>
                </form:select></td>
        </tr>
        <tr>
            <td><form:label path="prospectDto.company">Company:</form:label></td> 
            <td><form:select path="prospectDto.company"> 
                        <form:option value="">-- Select Company --</form:option> 
                        <c:forEach items="${listComp}" var="company"> --%>
                            <form:option value="${company.id}">${company.name}</form:option> 
                        </c:forEach> 
            </form:select></td> 

        </tr>
        <tr>
            <td colspan="4"><input type="submit" value="AddProspect"
                /></td>
        </tr>
    </table>
</form:form>
 </body>

</html>

我有一个公司列表,它在控制器类中,当我将它发送到jsp文件时,它可以工作。但是,在浏览器中,当我从组合框中选择一个公司时,它应该向控制器类的“doSave”方法发送包含公司的Prospect类型的modelAttribute,但它不起作用! 可能是什么问题?

0 个答案:

没有答案