一、SpringMVC环境搭建以及第一个小程序
相信我们在学习SpringMVC之前,都知道SpringMVC在开发中就是充当了我们之前所学的servlet,也就是控制器的作用。现在我们来看看,如何搭建一个简单的springMVC环境把。
一、首先仍然是准备我们所需的jar包:(没有jar包的同学可以加QQ群获得:1046132435)这里为大家提供编译软件以及所需jar包
spring-aop.jar
spring-bean.jar
spring-context.jar
spring-core.jar
spring-web.jar
spring-webmvc.jar
commons-logging.jar
二、准备完以上jar包,我们就可以着手搭建srpingMVC环境了。
思路:搭建环境是为了将我们之前Servlet所具有的功能以及权限交给SpringMVC来处理。
首先建立一个web程序。这里博主使用的servlet的版本是2.5,为了给大家更加清楚的看明白配置过程
接着我们把jar复制在lib目录下,并添加至内路径
然后我们来配置springmvc的配置文件,和配置spring的配置文件类似。在src目录下新建一个springmvc.xml文件(这里我使用的开发工具是STS,想获取的同学加上面那个qq群,也可以去STS官网下载)
然后我们在Namespaces中选中如下选项
接下来我们来在web.xml文件中进行配置,是为了将servlet的请求权限交给springMVC来处理,:我们通过以下配置,拦截所有请求,交给SrpingMVC来处理: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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SpringMvcProject</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>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- Tomact启动时候以第一个身份自动启动 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
紧接着我们创建一个handler包,里面创建一个类,我们之前知道一个普通的类要想变成具有特定功能的类,可以继承,实现接口,使用注解,配置等方式,下面使用的是注解方法:
@Controller的意思可以把这个类变成一个控制器,
@RequestMapping("welcome")是指我们需要拦截的请求路径。
package handler; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class SpringMVCHandler { @RequestMapping("welcome") public String welcome() { return "success";//view/success.jsp } }
接下来我们看springmvc.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:aop="http://www.springframework.org/schema/aop" 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.3.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.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 添加注解必须添加扫描包 --> <context:component-scan base-package="handler"></context:component-scan> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/view/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/view/"></property> <property name="suffix" value=".jsp"></property> </bean>
这里是我们返回的视图的配置。
<property name="prefix" value="/view/"></property>这是前置,代表返回view文件夹下
<property name="suffix" value=".jsp"></property> 这是后置,代表我们去view文件夹下找后缀名为.jsp的文件
接下来看程序:
新建index.jsp文件:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="welcome">first springmvc</a>
</body>
</html>
view/success.jsp文件的代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> 跳转成功 </body> </html>
点击链接,然后
这样我们第一个springMVC的程序就完成啦!