如果代码写成这样,就表示跳转到页面 index.jsp
new ModelAndView("index.jsp");
所谓的视图定位,指的是代码还是写成这样,但是会跳转到 /WEB-INF/page/index.jsp
new ModelAndView("index");
new ModelAndView("index.jsp");
所谓的视图定位,指的是代码还是写成这样,但是会跳转到 /WEB-INF/page/index.jsp
new ModelAndView("index");
- 修改springmvc-servlet.xml
修改springmvc-servlet.xml,增加
其作用是把视图约定在 /WEB-INF/page/*.jsp 这个位置<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/" /> <property name="suffix" value=".jsp" /><br data-filtered="filtered"></bean>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/index">indexController</prop> </props> </property> </bean> <bean id="indexController" class="controller.IndexController"></bean> </beans>
- 修改IndexController
把IndexController类的这一行代码
修改为ModelAndView mav = new ModelAndView("index.jsp");
ModelAndView mav = new ModelAndView("index");
package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class IndexController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = new ModelAndView("index"); mav.addObject("message", "Hello Spring MVC"); return mav; } }
- 移动index.jsp
在WEB-INF下新建目录page
把index.jsp移动到 WEB-INF/page 目录下
- 测试
访问页面
http://127.0.0.1:8080/springmvc/index