【问题标题】:Link to / goes to Apache root链接到/转到 Apache 根目录
【发布时间】:2016-08-17 15:40:58
【问题描述】:

这里是controller

@Controller
public class HomeController {

    @Autowired
    private UserDAO userDao;

    @RequestMapping("/")
    public ModelAndView accueil() throws Exception {
        List<User> listUsers = userDao.list();
        ModelAndView model = new ModelAndView("UserList");
        model.addObject("userList", listUsers);
        return model;
    }

    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public ModelAndView newUser() {
        ModelAndView model = new ModelAndView("UserForm");
        model.addObject("user", new User());
        return model;      
    }

    ...

}

jsp 中,我在链接中放置了一个按钮,以便在单击时调用controller 中的“accueil”操作:

<%@ 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">
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
...
<tr>
                <td>
                    <input type="submit" value="Save">
                </td>
                <td><a href="/"><input type="button" value="Annuler" /></a></td>
            </tr>        
            </form:form>
...

问题是,当我单击“Annuler”按钮时,我会到达 localhost:8080 !那么如何正确编写链接目标呢?

【问题讨论】:

    标签: spring jsp spring-mvc jsp-tags


    【解决方案1】:

    如果应用程序不是根应用程序,您需要将应用程序的上下文路径添加到 URL。

    使用

    href="${pageContext.request.contextPath}/"
    

    或将 JSTL 核心库添加到您的 JSP 中,然后使用

    href="<c:url value='/' />"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      相关资源
      最近更新 更多