zoukankan      html  css  js  c++  java
  • spring MVC心得(一)

    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/j2ee" 
                xmlns:javaee="http://java.sun.com/xml/ns/javaee" 
                xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
     
     <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
      <servlet>
          <servlet-name>Dispatcher</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:springMvc3-servlet.xml</param-value>
          </init-param>
      </servlet>
      <servlet-mapping>
          <servlet-name>Dispatcher</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>  
    </web-app>

    applicationContext.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:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="   
               http://www.springframework.org/schema/beans   
               http://www.springframework.org/schema/beans/spring-beans-3.1.xsd   
               http://www.springframework.org/schema/context   
               http://www.springframework.org/schema/context/spring-context-3.1.xsd  
               http://www.springframework.org/schema/mvc   
               http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
    
        
        <!--  <bean id="test" class="springMVC.Test01.login"/>
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
           <property name="mappings">
            <props>
               <prop key="test1.jsp">test</prop>
            </props>
         </property>
        </bean>
        
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        -->
    </beans>

    springMvc3-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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
            http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
        
        <!-- 启动扫描所有的controller 
    
    
      <mvc:annotation-driven />
        
      
     -->
        <!-- 通过component-scan 让Spring扫描org.swinglife.controller下的所有的类,让Spring的代码注解生效 -->  
        <context:component-scan base-package="springMVC.Test01"></context:component-scan>  
        
            <!-- 拦截器 
    <mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    </mvc:interceptors>
        -->
        
        <!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在jsp/目录下,查找XXX.jsp文件-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        
        <bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="defaultEncoding">
                <value>UTF-8</value>
            </property>
            <property name="maxUploadSize">
                <value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
            </property>
            <property name="maxInMemorySize">
                <value>4096</value>
            </property>
        </bean>
        
    </beans>

    Controller代码:

    package springMVC.Test01;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller  
    public class Login {
        @RequestMapping(value="/jsp/test1")  
           public ModelAndView login(){  
            //验证传递过来的参数是否正确,否则返回到登陆页面。  
           
                //指定要返回的页面为succ.jsp  
                ModelAndView mav = new ModelAndView("/jsp/test1");  
                //将参数返回给页面  
                mav.addObject("userName","guoyong");  
                mav.addObject("passWord", "guoyong123");  
                return mav;  
      
        } 
    }
  • 相关阅读:
    C# asp:Repeater DataSource List<T>
    MySQL DATE_FORMATE函数内置字符集的坑_转小叶子爹
    MySQL count(distinct) 逻辑的一个bug
    org.hibernate.PersistentObjectException: detached entity passed to persist:
    CGLIB Enhancement failed
    firstResult/maxResults specified on polymorphic query;
    Last packet sent to the server was 0 ms ago.
    MySql Error Code: 2006 – MySQl
    InnoDB: Error: auto-extending data file ./ibdata1 is of a different size
    mysql 大数据量分页处理
  • 原文地址:https://www.cnblogs.com/engine/p/4362326.html
Copyright © 2011-2022 走看看