zoukankan      html  css  js  c++  java
  • SpingMVC实现操作的第一方式

    (一)

    使用SpringMVC框架的步骤

    (1):在web.xml中配置前端控制器

    (2):在Spring-servlet.xml中配置

        1.  配置处理器映射器HandlerMapping(处理器Handler)
        2. 配置处理器适配器HandlerAdapter
        3. 配置视图解析器ViewAdapter

    (3):编写Controller

    (4):编写View.jsp页面

    (二)

    例子:

    1.FirstController.java

    package com.inspur.controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    
    
    public class FirstController implements Controller {
    
        @Override
        public ModelAndView handleRequest(HttpServletRequest arg0,
                HttpServletResponse arg1) throws Exception {
            // TODO Auto-generated method stub
            
            //创建ModelAndView对象
            ModelAndView mav = new ModelAndView();
            //向模型对象中添加数据
            mav.addObject("msg","first springmvc");
            // 设置逻辑试图名
            mav.setViewName("first");
            
            
            return mav;
        }
    
    }

    2.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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" >
    
        <!-- 配置处理器Handle-->
        <bean name="/firstController" class="com.inspur.controller.FirstController">
        </bean>
        <!-- 处理映射器,将处理器Handle的name作为url进行查找 -->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
        <!-- 视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="ViewClass" value="org.springframework.web.servlet.view.JstlView"></property>
            <property name="prefix" value="/WEB-INF/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    </beans>

    3.web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <display-name></display-name>
      <servlet>
          <!--  配置前端过滤器 -->
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
           <!-- 表示容器再启动的时候立即记载Servlet -->
           <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>springmvc</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
     
      
          
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    4.

    springmvc-servlet.xml的路径必须在WEB-INF下面,如果不在这里,可以在web.xml中servlet中配置

     <!-- 初始化加载配置文件  -->
        <init-param>
            <param-name>ContextConfigLocation</param-name>
            <param-value>classpath:springmvc-config.xml</param-value>
        </init-param>      

    5.

     6.解决post请求的中文乱码问题,在web.xml中配置以下代码

      <!-- 解决post请求中文乱码问题 -->
      <filter>
          <filter-name>CharaterEncodingFilter</filter-name>
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      </filter>
      <filter-mapping>
          <filter-name>CharaterEncodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
  • 相关阅读:
    Xdebug
    单点登录
    一个Https网站发送Http的 ajax请求的解决方法
    js关闭微信浏览器页面
    标准的身份证验证(第18位校验码)
    Redis 更新(set) key值 会重置过期时间问题
    PHP 报错:Deprecated: Methods with the same name as their class will not be constructor...
    php防sql注入
    web开发原则
    fopen()函数
  • 原文地址:https://www.cnblogs.com/sunxiaoyan/p/9177052.html
Copyright © 2011-2022 走看看