문제

I have got webapp in spring 3 mvc. The case is that I have index page with url, when user click on them should be display another page with details of choosed information. Now the details page is shown but without any information (on index page is creating model with correct variable but not in details controller - in debug mode). index controller method:

@RequestMapping(value="/{site}", method = RequestMethod.GET)
public String showDetails(@RequestParam(value = "site", required = true) String  site, Model model){
    Catalog product = catalogEndpoint.getByTitle(site);
    model.addAttribute("product", product);
    return "details";
    }

index html:

<form action="#" th:object="${product}" method="post" th:action="@{/details}">
    <table border="0" width="600"  th:each="sb, poz : ${product}" >
<tr >
<td rowspan="3" width="20"><span th:text="${poz.count}"></span></td> 
<td>
<a  th:href="@{/details/(site=${sb.tytul})}" th:value="${site}"><span  th:text="${sb.tytul}"></span></a>
    </td>
    </tr>
    <tr >
<td><span th:text="${sb.adres}"></span></td>
</tr>
<tr>
<td>category:<b><span th:text="${sb.category.name}"></span></b></td>
    </tr>
   </table>
    </form>

details controller method:

@RequestMapping(value = "details/{site}", method = RequestMethod.GET)
public String showHomePage(@PathVariable(value = "site") String site, Model model){
    model.addAttribute("product");
    return "details";
}

details html:

<form th:object="${product}" method="get" th:action="@{/details}">
    <table border="1" width="600"   >
<tr >
<td ><span th:text="${tytul}"></span></td> 
<td>
<span th:text="${opis}"></span>
    </td>
<td><span th:text="${adres}"></span></td>
</tr>
</table>
</form>

I don't have any ideas how to map the details site (I tried a lot of solution but nothing). Thanks for help.

도움이 되었습니까?

해결책

With thymeleaf, using th:object, you need to reference the fields of that object with *{}

<form th:object="${product}" method="get" th:action="@{/details}">
    <table border="1" width="600"   >
<tr >
<td ><span th:text="*{tytul}"></span></td> 
<td>
<span th:text="*{opis}"></span>
    </td>
<td><span th:text="*{adres}"></span></td>
</tr>
</table>
</form>

assuming tytul, opis, and adres are fields of product. Unless it's a type, don't forget

Catalog product = catalogEndpoint.getByTitle(site);
model.addAttribute("product", product);

in your details controller method, otherwise you won't have a Catalog model attribute.

다른 팁

inside your details controller where are you setting product object in your model ?

 model.addAttribute("product"); 

is just settingstring object "product", fetch the product object and set it in details controller like you have done in showDetails method

Change

model.addAttribute("product");

To

Catalog product = catalogEndpoint.getByTitle(site);
model.addAttribute("product", product);

in "showPage" method.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top