gpt4 book ai didi

java - 新的 ModelAndView 不起作用

转载 作者:行者123 更新时间:2023-11-30 03:56:13 28 4
gpt4 key购买 nike

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

在我的 Controller 类中,我有

@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
}
}

它进入登录方法,就好像我在返回其打印之前打印一些内容,但在返回语句时失败,并且我没有收到任何异常

在我的 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

最佳答案

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

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

现在所有库都已下载,我开始检查问题。首先,我必须在主 pom 中添加以下依赖项:

<!--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 文件并尝试启动该应用程序;我还必须通过添加以下测试来修改 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">

完成此操作后,我再次运行服务器,并且应用程序启动时没有错误;当我输入登录页面的网址时,com.jemmmedia.organise.service.impl.controller.LoginController.login(HttpSession, HttpServletResponse, HttpServletRequest, Model, FeedEntryBean, int, int, String)已成功调用;正如您所发生的那样,用于登录的 ModelAndView 为空这是控制台输出:

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

但是,即使 modelandview 为空,登录页面也会出现

所以基本上我强烈建议你检查映射器文件和 spring-security 文件

我希望这能有所帮助干杯,安吉洛

关于java - 新的 ModelAndView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23117797/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com