首页 > 学院 > 开发设计 > 正文

SpringMVC入门教程

2019-11-08 02:32:16
字体:
来源:转载
供稿:网友

1.使用maven创建web-app项目

2.添加所需要的jar包

在maven配置pom.xml添加一下属性

	<dependency>		<groupId>javax.servlet</groupId>		<artifactId>javax.servlet-api</artifactId>		<version>4.0.0-b01</version>	</dependency>	<dependency>		<groupId>org.sPRingframework</groupId>		<artifactId>spring-webmvc</artifactId>		<version>4.3.6.RELEASE</version>	</dependency>	<dependency>		<groupId>org.apache.commons</groupId>		<artifactId>commons-lang3</artifactId>		<version>3.5</version>	</dependency>3.修改web.xml文件

  <servlet>      <servlet-name>mvc-dispatcher</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <!-- DispatcherServlet对应的context m默认为/WEB-INF/$servlet-name$-servlet.xml -->      <init-param>      <!-- 改变 DispatcherServlet的默认配置-->	      <param-name>contextConfigLocation</param-name>	      <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>      </init-param>      <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>      <servlet-name>mvc-dispatcher</servlet-name>    <url-pattern>/</url-pattern>        </servlet-mapping>4.在WEB-INF/创建mvc-dispatcher-servlet.xml

<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"			//手动添加springMVC的支持       xmlns:p="http://www.springframework.org/schema/p"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd    //手动添加springMVC的支持           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">           <!-- 启用spring mvc注解 -->    <context:annotation-config></context:annotation-config>		<!-- HandlerMapping -->	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>	 	<!-- HandlerAdapter -->	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>	<context:component-scan base-package="cn"/>    //扫描cn包下的Spring注解配置	<mvc:annotation-driven /> 	<!-- ViewResolver -->	<bean		class="org.springframework.web.servlet.view.InternalResourceViewResolver">		<property name="viewClass"			value="org.springframework.web.servlet.view.JstlView" />		<property name="prefix" value="/WEB-INF/jsp/" />		<property name="suffix" value=".jsp" />	</bean></beans>5.在WEB-INF/新建jsp文件夹

并创建hello.jsp

6.创建类

package cn.luofeel.lmuke;//注意cn包import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/hello")public class HelloMVC {	// /hello/mvc	@RequestMapping("/mvc")	public String helloMvc(){		System.out.println("123");		return "home";	}	}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表