spring mvc返回空白页面!不报错!

叶落伤城1 2014-06-12 01:18:36
底下是整个项目的结构,用的maven搭建的,头次用很多东西不熟悉
下边是spring mvc的配置
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="xieguannan" />
<!-- <bean class="org.springframework.web.servlet.mvc.AnnotationMethodHandlerAdapter" /> -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>


下边是控制层方法的代码
package xieguannan.store.user.action;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import xieguannan.store.user.business.UserBusiness;
import xieguannan.store.user.entity.User;

@Controller
@RequestMapping("/user")
public class UserAction {
@Autowired
private UserBusiness userBusiness;
@RequestMapping("/list")
public String list(HttpServletRequest request){
System.out.println("test");
return "index";
}

@RequestMapping("add")
public ModelAndView add(){
System.out.println("add");
return new ModelAndView("store/user/createUser");
}

@RequestMapping(value="createUser",method=RequestMethod.POST)
public String createUser(User user){
userBusiness.createUser(user);
return "index";
}
}


现在问题是启动服务器以后,访问http://localhost:8080/mywebsite/user/list.do ,控制台会打印出test字符,说明已经进入相应的方法了,配置是没有问题的!但是问题就是每次什么错误都不报,返回页面就是空,什么都没有!这是为什么啊?研究半天还没找到问题,求大神指点。。配置文件里路径我都改了不知道多少次了,就是prefix那个属性后的。
页面代码很简单.
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
...全文
1324 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶落伤城1 2014-06-12
  • 打赏
  • 举报
回复
引用 2 楼 wpq139234 的回复:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix" value="WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> 由于你配置文件制定的jsp页面默认地址是 jsp下面 而你的返回地址是index 那么就解析成 jsp/index.jsp 你的项目部署图来看 jsp下面没有index.jsp 。按道理会报404错误的。 public String list(HttpServletRequest request){ System.out.println("test"); return "store/index"; } 楼上正解,本人详解 哈哈
我这里网络不太好,你写的那个路径我之前写过,但是不对,还是这个问题。而且你说返回404错误,但实际情况是我这里什么都没有返回,是一个空的HTML页面。。。我很费解,谢谢啊~~
叶落伤城1 2014-06-12
  • 打赏
  • 举报
回复
引用 1 楼 longtian1213 的回复:
你的index.jsp文件应该是在jsp/store下吧! 你应该返回store/index
晕,刚看见,我那个项目文档截图挂了。。。 你说的这个路径我也试过,不对。看下add的方法,返回的是store/user/createUser,这个路径是没错的,但是也是返回空页面。 截图只能晚上再发了,不在这台电脑上。。我MAVEN创建的项目,add所在页面在的路径是src/main/web-inf/jsp/store/user/createUser.jsp。。 谢谢你!
anythingforyou 2014-06-12
  • 打赏
  • 举报
回复
楼上的都是大牛,受教了
qq591628420 2014-06-12
  • 打赏
  • 举报
回复
受教
叶落伤城1 2014-06-12
  • 打赏
  • 举报
回复
找到问题所在了。。。。。。。。。。 问题现象是这样子的,只要你写了两级,比如@RequestMapping(”/user/list.do"),就会报一个404错误,这个404错误总是/项目名/user/WEB-INF/jsp/index.jsp,我就奇怪这个WEB-INF前边的/user/是怎么出来的。。把RequestMapping写成('/list.do')就没问题了。。 最后发现原来是spring-servlet中的prefix里边,应该写"/WEB-INF/jsp“,我少写了一个"/"。粗心大意啊。。。。。。。。。
叶落伤城1 2014-06-12
  • 打赏
  • 举报
回复
引用 7 楼 LS1firesoar 的回复:
你是所有页面返回的都是空白页面吗
你把web.xml发一下
比较怀疑你spirng 拦截是不是/* ,另外你看下日志 最简单就是发日志了
你用的容器室tomcat还是Jboss?

发现为什么是空白页了。。。web.xml配置文件里边写了个errorpage,404转到一个页面。。结果没做那个页面,现在返回404了倒是。。。到底路径错哪里了。。这个MAVEN创建的项目目录和平常的不一样,是不是和这个有关系啊?
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:**/applicationContext*.xml;
</param-value>
</context-param>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- <error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page> -->
</web-app>

我的index.jsp所在路径E:\workspaces\mywebsite\src\main\webapp\WEB-INF\jsp\index.jsp

报的错。。。。
HTTP Status 404 - /mywebsite/user/WEB-INF/jsp/index.jsp

type Status report

message /mywebsite/user/WEB-INF/jsp/index.jsp

description The requested resource is not available.

Apache Tomcat/7.0.47
疯狂的玖号 2014-06-12
  • 打赏
  • 举报
回复
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix" value="WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> 由于你配置文件制定的jsp页面默认地址是 jsp下面 而你的返回地址是index 那么就解析成 jsp/index.jsp 你的项目部署图来看 jsp下面没有index.jsp 。按道理会报404错误的。 public String list(HttpServletRequest request){ System.out.println("test"); return "store/index"; } 楼上正解,本人详解 哈哈
S117 2014-06-12
  • 打赏
  • 举报
回复
你的index.jsp文件应该是在jsp/store下吧! 你应该返回store/index
LS1firesoar 2014-06-12
  • 打赏
  • 举报
回复
你是所有页面返回的都是空白页面吗 你把web.xml发一下 比较怀疑你spirng 拦截是不是/* ,另外你看下日志 最简单就是发日志了 你用的容器室tomcat还是Jboss?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧