新的ModelAndView无法正常工作

时间:2014-04-16 18:51:07

标签: java spring jsp maven spring-mvc

我正在尝试运行springMVC应用程序,但它向我显示空白页。

在我的控制器课程中,我有

@Controller
public class LoginController extends CommonController {

    @RequestMapping("/login")
        public ModelAndView login(HttpSession session, HttpServletResponse response, HttpServletRequest request, Model model) {
         logger.info("something"); // printing something in logs
         return new ModelAndView("login");//But failing here not loading login.jsp
    }
}

它的内部登录方法就好像我在返回打印之前打印了一些内容但是在return语句中失败并且我没有得到任何异常

在我的spring-servlet.xml中。我有

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.jemmmedia.organise" />

    <!-- Enable annotation driven controllers, validation etc... -->
    <mvc:annotation-driven />

--------
-------

<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"
       p:prefixJson="true" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"  value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
 </beans>

我的login.jsp位于/core/src/main/webapp/WEB-INF/jsp/login.jsp

在pom.xml里面我有

<properties>
        <spring.version>3.0.7.RELEASE</spring.version>
    </properties>
<dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <!--spring -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.security</groupId>
                    <artifactId>spring-security-web</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.security</groupId>
                    <artifactId>spring-security-config</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.2</version>
        </dependency>

我的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>mvnproject</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
    <listener-class>
      org.springframework.security.web.session.HttpSessionEventPublisher
    </listener-class>
  </listener>

  <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-servlet.xml,
            /WEB-INF/spring-security.xml
        </param-value>
    </context-param>

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

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

    <error-page>
        <error-code>404</error-code>
        <location>/error404</location>
    </error-page>

    <error-page>
        <error-code>403</error-code>
        <location>/error403</location>
    </error-page>

    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/exception</location>
    </error-page>

我的日志设置为调试级别,似乎没有错误,日志中的一些最后文件是

'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
[ INFO] 2014-04-16 21:53:53 Mapped URL path [/files/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#1'
[ INFO] 2014-04-16 21:53:53 Mapped URL path [/profileImages/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#2'
[ INFO] 2014-04-16 21:53:53 Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'
[ INFO] 2014-04-16 21:53:53 Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#1'
[ INFO] 2014-04-16 21:53:53 Starting beans in phase 2147483647
[ INFO] 2014-04-16 21:53:53 Starting Quartz Scheduler now
[ INFO] 2014-04-16 21:53:53 FrameworkServlet 'spring': initialization completed in 670 ms

3 个答案:

答案 0 :(得分:2)

我在工作中遇到了一些问题;现在我能够检查你的代码;在这里我的步骤:

  • 我下载了github源代码
  • 为了下载所有需要的库,我执行了mvn clean install

现在已经下载了所有库,我开始检查问题。首先在主要的pom我必须添加以下dependendcies:

<!--Angelo-->
<dependency>
    <groupId>org.simpleframework</groupId>
    <artifactId>simple-xml</artifactId>
    <version>2.7.1</version>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.9</version>
</dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>  
<!--Fine Angelo-->

通过添加这些依赖项,一切编译得很好。但问题并没有在这里完成;在iBatis映射器文件中你有很多错误的类名。我猜你(或其他人)做了模型类的重构并忘记更新映射器文件(我想知道这个代码如何在你的tomcat上运行)

所以我修改了所有的XML文件,并尝试启动应用程序;我还必须通过添加此intestation来修改spring-security.xml文件:

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

一旦我这样做,我再次运行服务器,应用程序启动时没有错误;当我输入登录页面的URL时,com.jemmmedia.organise.service.impl.controller.LoginController.login(HttpSession, HttpServletResponse, HttpServletRequest, Model, FeedEntryBean, int, int, String)已成功调用;并且因为它发生在您身上,用于登录的ModelAndView为null 这是控制台输出:

18:48:06,932 INFO  [LoginController] model View: ModelAndView: reference to view with name 'login'; model is null

但是,如果modelandview为null,我会看到登录页面

所以基本上我强烈建议你去检查mappers文件和spring-security文件

我希望这可以提供帮助 干杯, 安吉洛

答案 1 :(得分:0)

我猜它以JSON格式返回结果;尝试用firebug检查反应;如果服务器响应是JSON格式,这与你有2个viewResolser的事实有关; yuo应该使用ContentNegotiationgViewResolver

答案 2 :(得分:0)

为什么你需要以下配置?

  

&LT;豆   类=&#34; org.springframework.web.servlet.view.json.MappingJacksonJsonView&#34;
  号码:prefixJson =&#34;真&#34; /&GT;

我认为删除上面的bean会很好。