0

您好我正在尝试对用户进行身份验证,但似乎它调用的是一个 jsp 页面而不是另一个控制器映射。

我的调度程序 servlet 是

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.beingjavaguys.domain.User</value>
            <value>com.beingjavaguys.domain.Chat</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        </props>
    </property>
</bean>



<bean id="hibernateTransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

web.xml的是

调度程序 org.springframework.web.servlet.DispatcherServlet 1

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

我的控制器是

@RequestMapping(value="/authenticate",method=RequestMethod.POST)
    public ModelAndView getAuthenticateResult(@ModelAttribute("user") User user,
            BindingResult result) {
        if(userService.authenticate(user))
        {
            return new ModelAndView("/userList");
        }
        else{
        return new ModelAndView("Login");
        }
    }

@RequestMapping(value="/userList", method=RequestMethod.GET)
    public ModelAndView getUserList() {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("chat", userService.getChat());
        return new ModelAndView("UserDetails", model);

    }

我正在authenticate.html使用方法从我的登录文件中调用,POST但我的问题是这个错误

HTTP Status 404 - /Spring-hibernate-integration-helloworld/WEB-INF/view/userList.jsp

type Status report

message /Spring-hibernate-integration-helloworld/WEB-INF/view/userList.html.jsp

description The requested resource is not available.

为什么它搜索 jsp 文件而不是将其重定向到控制器方法?但是如果我使用redirect:/userList.html它就可以了。它背后的逻辑是什么?

4

2 回答 2

2

如果您返回一个字符串,该字符串被解释为要呈现的视图的名称。视图的名称没有传递 a ViewResolver(在您的情况下可能是 a InternalResourceViewResolver),它将生成一个(内部)URL 以转发到。在这种情况下,这将是一个 JSP。

现在redirect:andforward:前缀是两种特殊情况。这redirect:将导致客户端重定向到该 URL,由于您的配置,该 URL 反过来会调用您的控制器。Aforward:在服务器端而不是客户端处理。

于 2013-09-04T07:44:47.977 回答
1

准确地说。其背后的逻辑是:

org.springframework.web.servlet.view.UrlBasedViewResolver

   /**
     * Overridden to implement check for "redirect:" prefix.
     * <p>Not possible in {@code loadView}, since overridden
     * {@code loadView} versions in subclasses might rely on the
     * superclass always creating instances of the required view class.
     * @see #loadView
     * @see #requiredViewClass
     */
    @Override
    protected View createView(String viewName, Locale locale) throws Exception {
        // If this resolver is not supposed to handle the given view,
        // return null to pass on to the next resolver in the chain.
        if (!canHandle(viewName, locale)) {
            return null;
        }
        // Check for special "redirect:" prefix.
        if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
            String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length());
            RedirectView view = new RedirectView(redirectUrl, isRedirectContextRelative(), isRedirectHttp10Compatible());
            return applyLifecycleMethods(viewName, view);
        }
        // Check for special "forward:" prefix.
        if (viewName.startsWith(FORWARD_URL_PREFIX)) {
            String forwardUrl = viewName.substring(FORWARD_URL_PREFIX.length());
            return new InternalResourceView(forwardUrl);
        }
        // Else fall back to superclass implementation: calling loadView.
        return super.createView(viewName, locale);
    }
于 2013-09-04T08:18:35.273 回答