基于配置文件xml方式,

配置springmvc步骤:

1.在pom文件中引入jar包:   
 <!--导入springmvc的jar包-->
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
    <version>4.3.1.RELEASE</version>
  </dependency>
2. 在web.xml文件中添加servlet

 <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--配置springmvc.xml的路径-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
3. 创建Handler接口实现类,

		import org.springframework.web.servlet.ModelAndView;
	import org.springframework.web.servlet.mvc.Controller;
	
	/**
	 * Created by Intellij IDEA.
	 * User:  陈亚萌
	 * Date:  2019/9/3
	 */
	public class MyHandler implements Controller {
	    @Override
	    public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
	        //装在模型数据和逻辑视图
	        ModelAndView modelAndView=new ModelAndView();
	        //添加模型数据
	        modelAndView.addObject("name","tom");
	        //添加逻辑视图
	        modelAndView.setViewName("show");
	        return modelAndView;
	    }
	}

3. 在resources中创建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"
	       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	    <!--配置HandlerMapping,将url请求映射到Handler-->
	    <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	        <!--配置mapping-->
	        <property name="mappings">
	            <props>
	                <!--配置test请求对应的handler-->
	                <prop key="/test">testHandler</prop>
	            </props>
	        </property>
	    </bean>
	    <!--配置-->
	    <bean id="testHandler" class="MyHandler"></bean>
	    <!--配置视图解析器-->
	    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	        <!--配置前缀-->
	        <property name="prefix" value="/"></property>
	        <!--配置后缀-->
	        <property name="suffix" value=".jsp"></property>
	    </bean>
	</beans>

基于注解实现springmvc

  1. 同上

  2. 同上

  3. springmvc中的配置

     <?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"
            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">
     <!--将annotationHandler自动扫描到ioc容器-->
         <context:component-scan base-package="com.imooc.handler"></context:component-scan>
         <!--配置视图解析器-->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <!--配置前缀-->
             <property name="prefix" value="/"></property>
             <!--配置后缀-->
             <property name="suffix" value=".jsp"></property>
         </bean>
     </beans>
    
  4. 创建controller,并加注解

     	package com.imooc.handler;
     	
     	import org.springframework.stereotype.Controller;
     	import org.springframework.ui.Model;
     	import org.springframework.web.bind.annotation.RequestMapping;
     	import org.springframework.web.servlet.ModelAndView;
     	
     	import javax.jws.WebParam;
     	import java.util.Map;
     	
     	/**
     	 * Created by Intellij IDEA.
     	 * User:  陈亚萌
     	 * Date:  2019/9/3
     	 */
     	@Controller
     	public class AnnotationHandler {
     	
     	    /**
     	     * 业务方法:ModelAndView完成数据的传递和视图的解析
     	     */
     	    @RequestMapping("/model")
     	    public ModelAndView modelAndViewTest(){
     	        //创建ModelAndView对象
     	        ModelAndView modelAndView=new ModelAndView();
     	        //填充数据
     	        modelAndView.addObject("name","cym");
     	        //设置逻辑视图
     	        modelAndView.setViewName("show");
     	        return modelAndView;
     	    }
     	
     	    /*
     	    * 业务方法:使用Model传值,String进行视图解析
     	    * */
     	    @RequestMapping("modelAndTest")
     	    public String modelTest(Model model){
     	        //填充模型数据
     	        model.addAttribute("name","yyh");
     	        //设置逻辑视图
     	        return "show";
     	    }
     	    /*
     	    * 业务方法:Map传值:String,进行视图解析
     	    * */
     	    @RequestMapping("map")
     	    public String MapTest(Map<String,String> map){
     	        //设置模型数据
     	        map.put("name","cymhhh");
     	        //设置逻辑视图
     	        return "show";
     	
     	    }
     	
     	}
    

	 <!--处理中文乱码-->
	  <filter>
	    <filter-name>encodingFilter</filter-name>
	    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
	    <init-param>
	      <param-name>encoding</param-name>
	      <param-value>UTF-8</param-value>
	    </init-param>
	    <init-param>
	      <param-name>forceEncoding</param-name>
	      <param-value>true</param-value>
	    </init-param>
	  </filter>
	  <filter-mapping>
	    <filter-name>encodingFilter</filter-name>
	    <url-pattern>/*</url-pattern>
	  </filter-mapping>
	  <!--设置访问静态资源-->
	  <servlet-mapping>
	    <servlet-name>default</servlet-name>
	    <url-pattern>*.css</url-pattern>
	  </servlet-mapping>

记得在配置的时候什么地方加/*,什么地方不加 *

	<filter-mapping>
	    <filter-name>encodingFilter</filter-name>
	    <url-pattern>/*</url-pattern>
	  </filter-mapping>
 posted on 2019-09-03 18:03  ben跑的换行符  阅读(427)  评论(0编辑  收藏  举报