Categories

Tags

springMVC入门(二)

springMVC入门(一)中的事例是用注解方式实现的,下面使用非注解方式实现

需要改动的地方是dispatcher-servlet.xml和HomeController

dispatcher-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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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-3.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

        <!--<context:annotation-config/>-->
        <!--<mvc:annotation-driven/>-->
        <!--控制器所在的包-->
        <!--<context:component-scan base-package="com.mycontroller"/>-->

        <!-- 静态资源(js、image等)的访问 -->
        <!--<mvc:default-servlet-handler/>-->

        <!--非注解-->
        <bean name="/home/index" class="com.mycontroller.HomeController"/>


        <!--ViewResolver 视图解析器-->
        <!--用于支持Servlet、JSP视图解析-->
        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>

</beans>

HomeController内容

package com.mycontroller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;


public class HomeController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
                                      HttpServletResponse response) throws Exception {
        return new ModelAndView("index");
    }
}