学习笔记-SpringMVC(二)

SpringMVC详细教程
注解的方式:
1.修改IndexController

//2.注解方式:表示该类是一个控制器
@Controller
public class IndexController /* implements Controller 第一种方式 */ {
    // 表示路径/index会映射到该方法上
    @RequestMapping("/index")
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

        ModelAndView mav = new ModelAndView("index");
        mav.addObject("message", "hello Spring MVC");
        // 模型数据是 message,内容是 “Hello Spring MVC”
        return mav;
    }
}

2.修改springmvc-servlet.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-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="controller"/>
    <bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/page/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

3.测试,结果一样。

猜你喜欢

转载自blog.csdn.net/qq_36653267/article/details/79372588