【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”
自己按照网上写的一个简单入门的spring mvc,得不到正确的结果。controller类如下,本来实现的是在地址拦中输入http://localhost:8080/springmvctest/login/mm,会定向到login/hello.jsp页面,但是tomcat总是说找不到mm.jsp,也就是说这里转到的页面是mm.jsp,ModelAndView不起作用?这个是异常信息
type Status report
message /springmvctest/WEB-INF/view/login/hell.jsp
description The requested resource (/springmvctest/WEB-INF/view/login/hell.jsp) is not available.
页面代码hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'hello.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 欢迎您,<%=request.getAttribute("loginUser") %><br> </body> </html>
package com.spring.test; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.portlet.ModelAndView; import org.springframework.web.servlet.view.RedirectView; @Controller public class TestController { public TestController(){} @RequestMapping(value = "/login/{user}", method = RequestMethod.GET) public ModelAndView myMethod(HttpServletRequest request, HttpServletResponse response, @PathVariable("user") String user, ModelMap modelMap) throws Exception { modelMap.put("loginUser", user); return new ModelAndView("/login/hello", modelMap); } @RequestMapping(value="/welcome",method=RequestMethod.GET) public String registPost() { return "/welcome"; } }
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" 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"> <!-- 自动扫描bean,把作了注解的类转换为bean --> <context:component-scan base-package="com.spring.test" /> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" /> </beans>
p:prefix="/WEB-INF/view/"p:suffix=".jsp"/>
这句话的意思,就说,你ModelAndView返回的页面/login/hello。会自动的在前面加上/WEB-INF/view/ 还会自动的在后面加上.jsp
那么你的真实返回的地址其实是/WEB-INF/view/login/hello.jsp
http://localhost:8080/springmvctest/login/mm.jsp这应该是你真实的jsp的访问URL啊,当然要加上.jsp,你的配置文件中配置了模型的后缀的话,只是表示你通过Controller返回的不需要加.jsp
比如 return "XXXX/mm";
页面转向成功了,就是一直获取不到model的值,原来是引错类了!
被这个折腾了两三天,坑死我了.提供一个portlet的玩意,完全是让人混淆的