基于XML配置的springMVC小案例

前端之家收集整理的这篇文章主要介绍了基于XML配置的springMVC小案例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

基于XML配置的springMVC小案例

spring-mvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  6. xmlns:jee="http://www.springframework.org/schema/jee"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:aop="http://www.springframework.org/schema/aop"
  9. xmlns:mvc="http://www.springframework.org/schema/mvc"
  10. xmlns:util="http://www.springframework.org/schema/util"
  11. xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  12. xsi:schemaLocation="
  13. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  14. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  15. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  16. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
  17. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  18. http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
  19. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  20. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  21. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
  22. <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  23. <property name="mappings">
  24. <props>
  25. <prop key="/hello.do">helloController</prop>
  26. </props>
  27. </property>
  28. </bean>
  29. <bean id="helloController" class="controller.HelloController"/>
  30. <!-- 配置视图解析器 -->
  31. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  32. <property name="prefix" value="/WEB-INF/"/>
  33. <property name="suffix" value=".jsp"/>
  34. </bean>
  35. </beans>

HelloController.java

  1. package controller;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5.  
  6. import org.springframework.web.servlet.ModelAndView;
  7. import org.springframework.web.servlet.mvc.Controller;
  8.  
  9. /**
  10. * 二级控制器
  11. * 处理业务逻辑
  12. */
  13. public class HelloController
  14. implements Controller{
  15.  
  16. public ModelAndView handleRequest(
  17. HttpServletRequest req,HttpServletResponse res)
  18. throws Exception {
  19. System.out.println(
  20. "HelloController处理请求...");
  21. //hello是视图名,ViewResolver会将
  22. //视图名解析成真正的页面名称,比如
  23. //解析成hello.jsp。
  24. return new ModelAndView("hello");
  25. }
  26. }

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <servlet>
  8. <servlet-name>springmvc</servlet-name>
  9. <servlet-class>
  10. org.springframework.web.servlet.DispatcherServlet
  11. </servlet-class>
  12. <!-- DispatcherServlet的初始化方法
  13. 在执行时,会启动spring容器。 -->
  14. <init-param>
  15. <param-name>contextConfigLocation</param-name>
  16. <param-value>classpath:spring-mvc.xml</param-value>
  17. </init-param>
  18. <load-on-startup>1</load-on-startup>
  19. </servlet>
  20. <servlet-mapping>
  21. <servlet-name>springmvc</servlet-name>
  22. <url-pattern>*.do</url-pattern>
  23. </servlet-mapping>
  24. </web-app>

hello.jsp

  1. <h1>Hello,SpringMVC!</h1>

猜你在找的SpringMVC相关文章