使用注解开发springmvc

1、 导入相关jar包

 

2、 web.xml配置

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

3、 Controller

@Controller
@RequestMapping("/springmvc")
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("hello springmvc annotation");
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg", "hello springmvc annotation");
        mv.setViewName("hello");
        return mv;
    }
    
}

4、 springmvc配置--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: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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置视图渲染器 -->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <!-- 结果视图的前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 结果视图的后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 扫描该包下的注解 -->
    <context:component-scan base-package="com.yxxy.controller"></context:component-scan>
</beans>

 

posted @ 2018-07-08 18:51  莯汐  阅读(82)  评论(0)    收藏  举报