SpringMVC之HanderMapping-3種訪問Controller的方式

KeepTing發表於2017-01-21

1、根據beanname找到對應Conntroller

<!-- 配置HandlerMapping  根據benaname找到對應Conntroller-->
  
	<bean
		class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean>
	
	  配置Controller
	<bean  name="/user.do" class="com.xfh.controler.UserController"></bean>


2、根據簡單URL查詢Controller

<!-- 根據簡單URL查詢Controller -->

	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/userInfo.do">userController</prop>
			</props>
		</property>
	</bean>
	<bean id="userController" name="/user.do" class="com.xfh.controler.UserController"></bean>


3、根據控制類的類名訪問Controller,訪問時類的類名需要小寫

<!--根據控制類的類名訪問Controller,訪問時類的類名需要小寫 -->	
	<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
	<bean class="com.xfh.controler.UserController"></bean>



UserController:

/**
 * 控制器
 */
public class UserController extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("===訪問到了===");
		return null;
	}

}

user.jsp:

<body>
    	<a href="user.do">根據BeanName訪問Controller</a><br>
    	<a href="userInfo.do">根據URL訪問Controller</a><br>
    	<a href="userController.do">根據Controller類名訪問Controller</a><br>
  </body>


web.xml:

 <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>




相關文章