1. 程式人生 > >springMVC——路徑重複問題

springMVC——路徑重複問題

在springMVC中不同的控制層中使用了相同的註解時,啟動web容器就會報錯

loginA

package com.controller.a;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginA {

	@RequestMapping("/login")
	public String login(){
		System.out.println("login_____A");
		return "index.jsp";
	}
}

loginB

package com.controller.b;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginB {

	@RequestMapping("/login")
	public String login(){
		System.out.println("login_______B");
		return "index.jsp";
	}
}

由於使用了相同的註解@RequestMapping("/login"),啟動容器時報錯

java.lang.IllegalStateException: Cannot map handler 'loginB' to URL path [/login]: There is already handler of type [class com.controller.a.LoginA] mapped.

解決方式是在類的上方使用@RequestMapping新增父路徑,這時方法中的路徑就是類級別的,而且return就要新增../才能正常訪問

loginA

package com.controller.a;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/a")
public class LoginA {

	@RequestMapping("/login")
	public String login(){
		System.out.println("login_____A");
		return "../index.jsp";
	}
}

loginB

package com.controller.b;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/b")
public class LoginB {

	@RequestMapping("/login")
	public String login(){
		System.out.println("login_______B");
		return "../index.jsp";
	}
}

使用以下路徑都能訪問

http://localhost:8080/springMVC_3_double/a/login.spring
http://localhost:8080/springMVC_3_double/b/login.spring

如果不想在return中新增../的方式,那麼需要在sringMVC-servlet.xml中新增以下程式碼,它的功能是限定預設訪問的是/根目錄

<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/" />
<?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">
        <context:component-scan base-package="com.controller.a"/>
        <context:component-scan base-package="com.controller.b"/>
		<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/" />
</beans>

 將控制層中return改為均能正常訪問

return "index.jsp";

loginA

package com.controller.a;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/a")
public class LoginA {

	@RequestMapping("/login")
	public String login(){
		System.out.println("login_____A");
		return "index.jsp";
	}
}

loginB

package com.controller.b;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/b")
public class LoginB {

	@RequestMapping("/login")
	public String login(){
		System.out.println("login_______B");
		return "index.jsp";
	}
}