springmvc练习

来源:互联网 发布:滴滴打车软件 编辑:程序博客网 时间:2024/06/06 00:24

本练习示例完整代码下载地址: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.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://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>


7、新建java文件(myController.java),内容如下:

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、项目结构图如下:



0 0
原创粉丝点击