本練習示例完整代碼下載地址:http://download.csdn.net/download/zhaoqingkaitt/9308245
1、開發環境:
Eclipse版本:Version: Mars Release (4.5.0)
jdk版本:1.7
tomcat:6.0
2、新建動態web項目(SpringMvc01),導入如下jar包
commons-logging-1.1.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-context-support-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
3、web.xml配置
<!--configure the setting of springmvcDispatcherServlet and configure the
mapping -->
<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:springmvc-servlet.xml</param-value>
</init-param>
<!-- <load-on-startup>1</load-on-startup> -->
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
4、在WEB-INF下新建文件夾jsp,在jsp文件夾中新建hello.jsp文件
5、新建springmvc-servlet.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--自動掃描-->
<context:component-scan base-package="test"></context:component-scan>
<!--視圖轉發配置 前綴+return+後綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--請求前綴-->
<property name="prefix" value="WEB-INF/jsp/"></property>
<!--請求後綴-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
package test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
http://localhost:8080/SpringMvc01/hello
* @author zhtt
*
*/
@Controller
//@RequestMapping("/mvc")
public class mvcController {
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
8、項目結構圖如下: