控制器之间的帖子路由问题:加载了右页但URL错误

时间:2017-04-06 15:50:28

标签: java spring spring-mvc spring-boot thymeleaf

当我点击我的第一页提交按钮时,会显示正确的目标页面,但网址错误。它仍然是第一页的网址:确实显示的htm页面是正确的:topologieInstanceCouloir.html但网址是http://localhost:8990/bus/topologie而不是http://localhost:8990/bus/topologieInstanceCouloir。这就是为什么不调用createForm的原因。你知道为什么会这样吗?

我在allTypesCle元素中有一个列表modelAttribute,在我第一次显示我的网页时没有填充。但是当我提交表单时,使用addCouloir方法可以很好地填充和显示列表。

我通过spring boot使用Spring 4.2。

这是以前的控制器代码,主要是有效形式(提交方法)路由到我的问题所在的htm页面。

@Controller
@SessionAttributes(value = "topologie", types = { Topologie.class })
@RequestMapping("/bus/topologie")
public class TopologieController {

    private static final String VIEW_TOPOLOGIE = "topologie";
    private static final String VIEW_TOPOLOGIE_INSTANCECOULOIR = "topologieInstanceCouloir";
...
@RequestMapping(method = RequestMethod.GET)
public String createForm(
        @RequestParam(value = "fileTopo", required = false) String id,
        @ModelAttribute("topologie") Topologie topologie, Model model)
                throws Exception {

    if (topologie == null) {
        topologie = new Topologie();
    }

    if (id != null) {
        loadTopoFromFile(topologie, id);
        topologie.setIsPassageCvs(Boolean.TRUE);
        final List<String> allVersionCadre = getAllCadreVersion(topologie);
        final List<String> allNatures = getAllNatures(topologie);
        model.addAttribute("allVersionCadre", allVersionCadre);
        model.addAttribute("allNature", allNatures);
    }

    model.addAttribute("topologie", topologie);
    return VIEW_TOPOLOGIE;
}

    @RequestMapping(method = RequestMethod.POST, params = { "definirInstance" })
    public String validForm(final Topologie topologie,
            final BindingResult result, RedirectAttributes attr,
            HttpSession session, HttpServletRequest req) {

        logger.info("REST request to valid form : {}", topologie);
        topologieService.update(topologie);

        if (result.hasErrors()) {
            logger.info("Form has errors ! : {}", result);
            return VIEW_TOPOLOGIE;
        }

        attr.addFlashAttribute("topologie", topologie);
        return VIEW_TOPOLOGIE_INSTANCECOULOIR;
    }

这是第二个控制器代码:

@Controller
@SessionAttributes(value = "topologie", types = { Topologie.class })
@RequestMapping("/bus/topologieInstanceCouloir")
public class TopologieInstanceCouloirController {

private static final String VIEW_TOPOLOGIE_INSTANCECOULOIR = "topologieInstanceCouloir";

private final Logger logger = LoggerFactory
        .getLogger(TopologieInstanceCouloirController.class);

@ModelAttribute("allTypesCle")
public List<String> getAllTypesCle() {
    logger.info("ModelAttribute to get all types Cle");
    return typesCleService.getAll();
}

@RequestMapping(method = RequestMethod.GET)
public String createForm(
        @ModelAttribute("topologie") Topologie topologie, Model model)
                throws Exception {

    if (topologie == null) {
        topologie = new Topologie();
    }
    model.addAttribute("topologie", topologie);

    return VIEW_TOPOLOGIE_INSTANCECOULOIR;
}

@RequestMapping(method = RequestMethod.POST, params = { "addCouloir" })
public String addCouloir(@ModelAttribute("topologie") Topologie topologie,
        final Model model,
        final HttpServletRequest req) throws IOException {
    final String param = req.getParameter("addCouloir");
    logger.info("REST request to add Couloir : {}", param);

    return VIEW_TOPOLOGIE_INSTANCECOULOIR;
}

这是html代码:

    <form action="#" th:action="@{/bus/topologieInstanceCouloir}"
        th:object="${topologie}" method="post" class="form-horizontal">
...
    <div class="form-group" th:if="!${#lists.isEmpty(allTypesCle)}">
        <label for="typeCles" class="col-sm-2 control-label">Types
            de clé</label>
        <div class="col-sm-6">
            <div th:each="typeCle : ${allTypesCle}" class="checkbox">
                <label th:for="${#ids.next('typesCle')}"> <input
                    type="checkbox"
                    th:field="*{partitions[__${rowPartitionStat.index}__].instances[__${rowInstanceStat.index}__].typesCle}"
                    th:value="${typeCle}" class="checkboxTypeCles" /> <span
                    th:text="${typeCle}">...</span>
                </label>
            </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

看起来你想念

model.add("topologie", topologie);

在你的createForm方法

更新

尝试添加代码

@ModelAttribute("topologie")
public List<String> getAllTypesCle() {
    logger.info("ModelAttribute to get Topologie");
    return new Topologie();
}