怎么用Spring建立MVC模式
如何用Spring建立MVC模式
web.xml配置
spring-mvc.xml配置
spring-mvc-servlet.xml配置
BulletionListAction.java配置
web.xml配置
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml, /WEB-INF/spring-mvc-servlet.xml</param-value> </context-param> <!—servlet-name必须和context-param指定的xml相同 --> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
spring-mvc.xml配置
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="bulletinList.do">bulletionListAction</prop> <prop key="hello.do">helloWorldController</prop> </props> </property> </bean> <bean id="bulletionListAction" class="com.controller.BulletionListAction"></bean> <bean id="helloWorldController" class="com.test.HelloWorldController"></bean>
spring-mvc-servlet.xml配置
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.JstlView</value> </property> </bean>
BulletionListAction.java配置
public class BulletionListAction implements Controller{ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("BulletionListAction执行成功"); return new ModelAndView("index.jsp"); } }