1. 程式人生 > >Spring MVC的配置使用(不使用註解)

Spring MVC的配置使用(不使用註解)

流程:


1.配置spring MVC開發環境

---引入ioc,webmvc開發包

---src中新增applicationContext.xml配置檔案

2.編寫Controller

public class HelloController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView();
mav.setViewName("hello");
mav.getModel().put("msg", "模型資料");
return mav;
}

}

3.編寫hello.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  
  <body>
    <%=request.getAttribute("msg") %>
  </body>

</html>

4.配置web.xml(配置DispatcherServlet)

<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:applicationContext.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>

5.配置applicationContext.xml(配置handlermapping、HelloController、ViewResolver)

<bean id="handlermapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

    <property name="mappings">

      <props>
        <prop key="/hello.do">helloController</prop>
      </props>
    </property>
  </bean>
  <bean id="helloController" class="controller.HelloController"></bean>
  <bean id="viewresolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/"></property>
    <property name="suffix" value=".jsp"></property>

  </bean>

6.實現效果