SpringMVC (注解的映射器和适配器)

SpringMVC的构建:https://blog.csdn.net/young_1004/article/details/82114221

1.在springmvc.xml 中配置注解的处理器映射器

<!--3.1之后注解的处理器映射器-->

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

2.在springmvc.xml 中配置注解的处理器适配器

<!--3.1之后注解的处理器适配器-->

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

3.配置视图解析器

<!--视图解析器-->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

 

4.开启注解扫描器

<context:component-scan base-package="com.uplooking.springmvc"></context:component-scan>

5.springmvc.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:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">



<!--处理器映射器-->

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

<!--3.1之后注解的处理器映射器-->

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

<!--处理器适配器-->

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>

<!--3.1之后注解的处理器适配器-->

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

<!--视图解析器-->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

<!--开启注解扫描器-->

<context:component-scan base-package="com.ma.springmvc"></context:component-scan>

</beans>

6.Handler

package com.ma.springmvc.handler;



import org.springframework.stereotype.Controller;

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

import org.springframework.web.servlet.ModelAndView;



@Controller

public class UserController {

@RequestMapping("/findUser")

public ModelAndView findUser(){

ModelAndView modelAndView = new ModelAndView();

modelAndView.setViewName("index.jsp");

modelAndView.addObject("username","User");

return modelAndView;

}



}

7.index.jsp

<html>

<body>

<h2>${username}</h2>

</body>

</html>

8.访问

http://localhost:8080/findUser.action

9.效果

猜你喜欢

转载自blog.csdn.net/young_1004/article/details/82115563
今日推荐