javax.servlet.ServletException:无法解析名称为' contacts / show'的视图在名为'调度程序的servlet中

时间:2017-05-30 22:52:29

标签: spring spring-mvc tiles-3

我正在使用Spring MVC。我用tiles创建了两个带有身份名称的身份项目。其中一个工作正常,但其他抛出异常 javax.servlet.ServletException: Could not resolve view with name 'contacts/show' in servlet with name 'dispatcher。我该如何解决这个问题?

UPD:

控制器:

package ua.train.superproject.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import ua.train.superproject.model.Contact;
import ua.train.superproject.service.ContactService;
import ua.train.superproject.util.Message;
import ua.train.superproject.util.UrlUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

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

    @Autowired
    ContactService contactService;

    @Autowired
    MessageSource messageSource;

    @RequestMapping(method = RequestMethod.GET)
    public String index(Model model) {
        return "redirect:/contacts";
    }

    @RequestMapping(value = "contacts", method = RequestMethod.GET)
    public String homePage(Model model) {
        model.addAttribute("contacts", contactService.findAllWithDetail());
        return "contacts/list";
    }

    @RequestMapping(value = "contacts/{id}", method = RequestMethod.GET)
    public String getById(@PathVariable("id") final Integer id, Model model) {
        model.addAttribute("contact", contactService.findById(id));
//        model.addAttribute( contactService.findAllWithDetail()
//                .stream().filter(a -> a.getId().equals(id))
//                .findFirst().orElse(null));
        return "contacts/show";
    }

    @RequestMapping(value = "contacts/{id}", params = "form", method = RequestMethod.POST)
    public String update(Contact contact, BindingResult bindingResult, Model model, HttpServletRequest httpServletRequest,
                         RedirectAttributes redirectAttributes, Locale locale) {
        if (bindingResult.hasErrors()){
            model.addAttribute("message", new Message("error",
                    messageSource.getMessage("contact_save_fail", new Object[]{}, locale)));
            model.addAttribute("contact", contact);
            return "contacts/update";
        }
        model.asMap().clear();
        redirectAttributes.addFlashAttribute("message", new Message("success",
                messageSource.getMessage("contact_save_success", new Object[]{}, locale)));
        contactService.save(contact);
        return "redirect:/contacts/" + UrlUtil.encodeUrlPathSegment(contact.getId().toString(),
                httpServletRequest);
    }

    @RequestMapping(value = "contacts/{id}", params = "form", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") Integer id, Model uiModel) {
        Contact contact = contactService.findById(id);
        /*.stream().filter(a -> a.getId().equals(id))
                .findFirst().orElse(null);*/
        uiModel.addAttribute("contact", contact);

        return "contacts/update";
    }

    @RequestMapping(value = "contacts", params = "form", method = RequestMethod.POST)
    public String create(Contact contact, BindingResult bindingResult, Model model, HttpServletRequest httpServletRequest,
                         RedirectAttributes redirectAttributes, Locale locale) {
        if (bindingResult.hasErrors()){
            model.addAttribute("message", new Message("error",
                    messageSource.getMessage("contact_save_fail", new Object[]{}, locale)));
            model.addAttribute("contact", contact);
            return "contacts/update";
        }
        model.asMap().clear();
        redirectAttributes.addFlashAttribute("message", new Message("success",
                messageSource.getMessage("contact_save_success", new Object[]{}, locale)));
        contactService.save(contact);
        return "redirect:/contacts/" + UrlUtil.encodeUrlPathSegment(contact.getId().toString(),
                httpServletRequest);
    }

    @RequestMapping(value = "contacts", params = "form", method = RequestMethod.GET)
    public String createForm(Model uiModel) {
        uiModel.addAttribute("contact", new Contact());
        return "contacts/create";
    }
}

的DispatcherServlet:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

        <mvc:annotation-driven/>

        <context:component-scan base-package="ua.train.superproject.web"/>

        <mvc:resources mapping="/resources/**" location="/resources/"/>

        <mvc:default-servlet-handler/>

        <bean id="tilesConfigurer"
              class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
            <property name="definitions">
                <list>
                    <value>/WEB-INF/layouts/general.xml</value>
                </list>
            </property>
        </bean>

        <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"
              id="tilesViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
        </bean>

        <mvc:interceptors>
            <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"
                  p:paramName="theme"/>
            <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
                  p:paramName="lang"/>
        </mvc:interceptors>

        <bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
              id="messageSource" p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application"
              p:fallbackToSystemLocale="false"/>

        <bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
              id="localeResolver" p:cookieName="locale" p:defaultLocale="en_HK"/>

        <bean id="themeSource"
              class="org.springframework.ui.context.support.ResourceBundleThemeSource">
            <property name="basenamePrefix" value="theme-"/>
        </bean>

        <bean id="themeResolver"
              class="org.springframework.web.servlet.theme.CookieThemeResolver">
            <property name="defaultThemeName" value="default"/>
        </bean>
    </beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>java-blog-aggregator</display-name>


    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>jdbc,mysql</param-value>
        <!--<param-value>hibernate,mysql</param-value>-->
        <!--<param-value>datajpa,mysql</param-value>-->
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

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

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

general.xml(瓷砖)

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
        "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
        "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
    <definition name="common" template="/WEB-INF/views/default.jspx">
        <put-attribute name="footer" value="/WEB-INF/views/footer.jspx"/>
        <put-attribute name="header" value="/WEB-INF/views/header.jspx"/>
        <put-attribute name="menu" value="/WEB-INF/views/menu.jspx"/>
    </definition>

    <definition name="contacts/list" extends="common">
        <put-attribute name="body" value="/WEB-INF/views/contacts/list.jspx"/>
    </definition>

    <definition name="contacts/show" extends="common">
        <put-attribute name="body" value="/WEB-INF/views/contacts/show.jspx"/>
    </definition>

    <definition name="contacts/update" extends="common">
        <put-attribute name="body" value="/WEB-INF/views/contacts/edit.jsp"/>
    </definition>

    <definition name="contacts/create" extends="common">
        <put-attribute name="body" value="/WEB-INF/views/contacts/edit.jsp"/>
    </definition>

</tiles-definitions>

堆栈跟踪:

javax.servlet.ServletException: Could not resolve view with name 'contacts/list' in servlet with name 'dispatcher'
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1262)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

有什么想法吗?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

您的瓷砖中没有瓷砖。 contacts / show