版权声明:如需转载,请注明出处,谢谢! https://blog.csdn.net/qq_41172416/article/details/82428074
第一步,导入jar包
注意:红色框中的jar包为,springMVC所需的jar包;其余为之前spring框架所需的jar包。
jar包地址:https://pan.baidu.com/s/1czUtOg7VRFAB0_VmlqhWJg
第二步,在web.xml中配置Servlet
<!-- 配置spring mvc的核心控制器DispatcherServlet -->
<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>
第三步,创建Spring MVC的配置文件(Spring-servlet.xml)
第一种:使用xml编写Spring-servlet.xml
1、Spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置处理器映射 --> <bean name="/index.html" class="cn.smbms.controller.IndexController" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
2、IndexController控制器
package com.bdqn.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class IndexController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { System.out.println("hello,Spring MVC"); return new ModelAndView("index"); } }
第二种:使用注解编写Spring-servlet.xml
前言:如果有多个控制器,就需要定义多个控制器的javaBean,这样业务太繁琐,故而,给大家介绍下面的这种配置。
1、Spring-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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 开启注解 --> <mvc:annotation-driven/> <!-- 扫描注解的控制器 --> <context:component-scan base-package="com.bdqn.controller"/> <!-- 完成视图的对应 --> <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
2、IndexController控制器
package com.bdqn.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/user") public class userController { @RequestMapping("/index.html") public String user(){ return "index"; } @RequestMapping("/test.html") public ModelAndView show(@RequestParam(value="userName",required=false,defaultValue="游客")String userName) { ModelAndView mView=new ModelAndView(); mView.addObject("userName", userName); mView.setViewName("success"); return mView; } }
到这搭建SpirngMVC就结束了!
演示案例:https://pan.baidu.com/s/1f3rcseFJP1u_X5DcY49dtQ
访问地址:http://localhost:8080/chapter09_001/user/index.html(注意:此项目用myeclips所创建)