Spring-MVC开发步骤(入门配置)-LMLPHP

Spring-MVC开发步骤(入门配置)-LMLPHP

Spring-MVC开发步骤(入门配置)

Step1.导包	spring-webmvc
Step2.添加spring配置文件
Step3.配置DispatcherServlet
在web.xml中:
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!-- DispatcherServlet的初始化方法会启动spring容器,
contextConfigLocation用来指定spring配置文件的位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.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> Step4.写自己的Controller实现spring框架中的Controller,重写handleRequest方法
/**
* ModelAndView有两个常用构造器
* ModelAndView(String viewName)
* ModelAndView(String viewName, Map data) viewName:视图名 data:处理结果
* */ Step5.写自己的jsp Step6.在spring配置文件中,添加HandlerMapping,ViewResolver配置
<!-- 配置HandlerMapping -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.do">helloController</prop>
</props>
</property>
</bean>
<!-- 配置Controller -->
<bean id="helloController" class="controller.HelloController"/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
04-15 06:07