Link 到/转到 Apache 根目录

Link to / goes to Apache root

这是 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 中,我在 link 中放置了一个按钮,这样当单击时,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 !那么如何正确编写 link 目标?

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

使用

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

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

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