1. 程式人生 > >Spring MVC 專案No mapping found for HTTP request with URI

Spring MVC 專案No mapping found for HTTP request with URI

/**
     * 跳轉到登入頁
     * http://localhost:8080/web-0.0.1-SNAPSHOT/index/index
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/index", method = { RequestMethod.GET })
    public ModelAndView index(HttpSession httpSession) {
    	
    	ModelAndView mav = new ModelAndView();
    	if(checkUser(httpSession)) {
            mav.setViewName("statis/index");
    	}else {
    		mav.setViewName("index/index");//使用簡寫進行jsp頁面跳轉,需要在applicationContext-mvc.xml檔案中配置跳轉到jsp檢視的前後綴
    	}
    	return mav;
    }

Controller層跳轉jsp檢視,如果使用簡寫方式跳轉,則需要在spring的xml中配置jsp的前後綴。

applicationContext-mvc.xml:

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

<beans ...>

<!-- 對模型檢視名稱的解析,即在模型檢視名稱新增前後綴 -->

<bean

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="viewClass"

  value="org.springframework.web.servlet.view.JstlView" />

<property name="prefix" value="/WEB-INF/views/" />

<property name="suffix" value=".jsp" />

</bean>

<!-- 靜態資原始檔,不會被Spring MVC攔截 -->

<mvc:resources location="/" mapping="/**/*.html"/>

<mvc:resources location="/"

mapping="/**/*.js"/>

<mvc:resources location="/" mapping="/**/*.css"/>

<mvc:resources location="/" mapping="/**/*.png"/>

<mvc:resources location="/" mapping="/**/*.jpg"/>

<mvc:resources location="/" mapping="/**/*.gif"/>

</beans>