qq_30511823 2017-12-11 13:57 采纳率: 50%
浏览 2573
已采纳

Spring MVC 视图问题,访问 jsp 却返回字符串,紧急求助!

war 包结构如下:

图片说明

spring-mvc 配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>

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"
xsi:schemaLocation="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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<!-- 注解扫描包 -->  
<context:component-scan base-package="com.lvmama.ptest.controller" />

<!-- 开启注解 -->

<!-- 定义跳转的文件的前后缀 ,视图模式配置-->  
<bean id="viewResolver"  
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->  
    <property name="prefix" value="/WEB-INF/jsp/" />  
    <property name="suffix" value=".jsp" />  
</bean>  

<!--  配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd   -->  
<mvc:resources mapping="/img/**" location="/img/" />  
<mvc:resources mapping="/js/**" location="/js/" />  
<mvc:resources mapping="/css/**" location="/css/" />  
<mvc:resources mapping="/html/**" location="/html/" />  

web.xml 配置如下:

<?xml version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

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

<!-- 加载Spring容器配置 -->  
<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>  

<!-- 设置Spring容器加载所有的配置文件的路径 -->  
<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <!-- <param-value>classpath:config/spring-*.xml</param-value> -->  
    <param-value>classpath:applicationContext-service.xml</param-value>  

</context-param>  

<!-- 配置SpringMVC核心控制器 -->  
<servlet>  
    <servlet-name>ptest_controller</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <!-- 配置初始配置化文件,前面contextConfigLocation看情况二选一 -->    
    <init-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring-mvc.xml</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
</servlet>  

<!--为DispatcherServlet建立映射 -->  
<servlet-mapping>  
    <servlet-name>ptest_controller</servlet-name>  
    <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->  
    <url-pattern>/</url-pattern>  
</servlet-mapping>  

<!-- 防止Spring内存溢出监听器 -->  
<listener>  
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
</listener>  

<!-- 解决工程编码过滤器 -->  
<filter>  
    <filter-name>encodingFilter</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>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>  

通过Controller 层访问jsp 页面,返回的是字符串“index”
图片说明

但是输入jsp 以外的请求,又能正常访问:
图片说明

请各位帮帮忙解答,十分感谢!!!!

对应的controller class 如下:
@Controller
public class PtestProjectController {
@Autowired
private PtestProjectService ptestprojectservice;

@RequestMapping("/project/list/{id}")
@ResponseBody
public PtestProject getProjectById(@PathVariable int id) throws Exception{
     PtestProject result = ptestprojectservice.getProjectById(id);
     return result;
    }

@RequestMapping("/index")
@ResponseBody
public String getIndex() throws Exception{
     return "index";
}
}

为什么不返回jsp页面?
  • 写回答

10条回答

  • 随身备纸巾 2017-12-12 02:15
    关注

    @RequestMapping("/index")
    @ResponseBody
    public String getIndex() throws Exception{
    return "index";
    }
    }
    把@ResponseBody去掉,这指定了字符串的形式响应,所以你返回的是index。
    上面那个正常是因为你返回的result是字符串,所以把result解析了到页面,下面这个你加了@ResponseBody,就把“index”当成一个字符串响应到页面上,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题