Spring 4.0 + Spring MVC(四)学习
2015-10-13 20:39
573 查看
1、在前面的二、三节的学习中分别提到了自动注入@Autowired和spring mvc的@Controller分发。在本节总,我将提高将两者结合起来使用。实例还是沿用第三节的Demo。
2、将web.xml中的内容新增如下:
在web.xml文件中提到bean.xml和spring-mvc.xml的内容分别如下2.1和2.2中:
2.1、其中bean.xml中的内容为:
2.2、其中spring-mvc.xml中的内容为:
2.3、在2.1中bean.xml中提到的com.hl.demo包中有一个Chinese类,如下所示:
2.4、在2.2中spring-mvc.xml中提到的com.hl.controller包中有一个myController类,如下:
3、通过上面的配置便将@Autowired和@controller合并起来,换言之就是spring+spring mvc的最简单用法,而其他的用法都是在此基础上扩展开发来的。
4、启动tomcat后,运行 http://localhost:8080/SpringStudy/myController/showHelloWorld.do,便可将运行起来,得到的结果如下所示:
5、在下一节中,我们将讲解为何这么配置。即去看分析源代码。
6、本实例的下载地址:http://download.csdn.net/detail/qq5132834/9178329
2、将web.xml中的内容新增如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringStudy</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean.xml</param-value> </context-param> <listener> <description>spring监听器</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring MVC 配置文件</description> <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> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
在web.xml文件中提到bean.xml和spring-mvc.xml的内容分别如下2.1和2.2中:
2.1、其中bean.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <context:component-scan base-package="com.hl.demo" /> </beans>
2.2、其中spring-mvc.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!-- controller包(自动注入) --> <context:component-scan base-package="com.hl.controller" /> </beans>
2.3、在2.1中bean.xml中提到的com.hl.demo包中有一个Chinese类,如下所示:
package com.hl.demo; import org.springframework.stereotype.Service; @Service("Chinese") public class Chinese { public void eat() { System.out.println("eating"); } public void walk() { System.out.println("walking"); } }
2.4、在2.2中spring-mvc.xml中提到的com.hl.controller包中有一个myController类,如下:
package com.hl.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.hl.demo.Chinese; @Controller @RequestMapping("/myController") public class myController { @Autowired private Chinese chinese; @RequestMapping("/showHelloWorld") public String showHelloWorld(){ System.out.println("----showLogin----"); chinese.eat(); chinese.walk(); return "../index.jsp"; } }
3、通过上面的配置便将@Autowired和@controller合并起来,换言之就是spring+spring mvc的最简单用法,而其他的用法都是在此基础上扩展开发来的。
4、启动tomcat后,运行 http://localhost:8080/SpringStudy/myController/showHelloWorld.do,便可将运行起来,得到的结果如下所示:
----showLogin---- eating walking
5、在下一节中,我们将讲解为何这么配置。即去看分析源代码。
6、本实例的下载地址:http://download.csdn.net/detail/qq5132834/9178329