RequestMapping不会重定向到正确的JSP

时间:2014-11-11 00:11:11

标签: jsp spring-mvc spring-4

正在发生的是home.jsp没有显示。当我输入URL,显示的页面时,JSP会正确显示。单击链接以创建新的,编辑或删除联系人时,URL会缩短。

http://localserver:8080/myproject/listContact 正常运行

点击编辑链接:

http://localserver:8080/editContact?id=1 获取404错误

主页JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Contact Manager Home</title>
    </head>
    <body>
        <div align="center">
            <h1>Contact List</h1>
            <h3><a href="/newContact">New Contact</a></h3>
            <table border="1">
                <th>No</th>
                <th>Name</th>
                <th>Email</th>
                <th>Address</th>
                <th>Telephone</th>
                <th>Action</th>

                <c:forEach var="contact" items="${listContact}" varStatus="status">
                <tr>
                    <td>${status.index + 1}</td>
                    <td>${contact.name}</td>
                    <td>${contact.email}</td>
                    <td>${contact.address}</td>
                    <td>${contact.telephone}</td>
                    <td>
                        <a href="/editContact?id=${contact.id}">Edit</a>
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="/deleteContact?id=${contact.id}">Delete</a>
                    </td>

                </tr>
                </c:forEach>             
            </table>
        </div>
    </body>
</html>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

    <display-name>SpringMvcJdbcTemplate</display-name>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.codejava.spring.config</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

MVC配置

@Configuration
@ComponentScan(basePackages={"com.codejava.spring"})
@EnableWebMvc
public class MvcConfiguration extends DelegatingWebMvcConfiguration{

    @Bean
    public ViewResolver getViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
     }

    @Bean
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://localhost:5432/contactdb");
        dataSource.setUsername("postgres");
        dataSource.setPassword("root");

        return dataSource;
    }

    @Bean
    public ContactDAO getContactDAO() {
        return new ContactDAOImpl(getDataSource());
    }



    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");

    }

家庭控制器

@Controller
@RequestMapping(value="/*")
public class HomeController {

    @Autowired
    private ContactDAO contactDAO;

    @RequestMapping(value="/listContact", method = RequestMethod.GET)
    public ModelAndView listContact(ModelAndView model) throws IOException{
        List<Contact> listContact = contactDAO.list();
        model.addObject("listContact", listContact);
        model.setViewName("home");

        return model;
    }

    @RequestMapping(value="/newContact", method = RequestMethod.GET)
    public ModelAndView newContact(ModelAndView model){
        Contact newContact = new Contact();
        model.addObject("contact", newContact);
        model.setViewName("ContactForm");
        return model;
    }

    @RequestMapping(value= "/saveContact", method = RequestMethod.POST)
    public ModelAndView saveContact(@ModelAttribute Contact contact) {
        contactDAO.saveOrUpdate(contact);
        return new ModelAndView("redirect:/listContact");

    }

    @RequestMapping(value="/deleteContact", method=RequestMethod.GET)
    public ModelAndView deleteContact(HttpServletRequest request){
        int contactId = Integer.parseInt(request.getParameter("id"));
        contactDAO.delete(contactId);
        return new ModelAndView("redirect:/listContact");
    }

    @RequestMapping(value= "/editContact", method=RequestMethod.GET)
    public ModelAndView editContact(HttpServletRequest request){
        int contactId = Integer.parseInt(request.getParameter("id"));
        Contact contact = contactDAO.get(contactId);
        ModelAndView model = new ModelAndView("ContactForm"); 
        model.addObject("contact", contact);
        return model;
    }

2 个答案:

答案 0 :(得分:0)

您应该将链接更改为

http://localserver:8080/myproject/editContact?id=1

答案 1 :(得分:0)

如果链接的网址为/editContact,则浏览器会请求http://<hostname>:<port>/editContact,因此您必须按如下方式为链接添加上下文路径

<td>
    <a href="${pageContext.request.contextPath}/editContact?id=${contact.id}">Edit</a>
    &nbsp;&nbsp;&nbsp;&nbsp;
    <a href="${pageContext.request.contextPath}/deleteContact?id=${contact.id}">Delete</a>
</td>

然后浏览器会请求http://<hostname>:<port>/<contextpath>/editContact

相关问题