springmvc

来源:互联网 发布:遥感影像数据拼接 编辑:程序博客网 时间:2024/06/01 15:04

springmvc使用
引入jar包之后,在web.xml文件中配置核心控制器

<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value></context-param><listener><description>spring监听器</description><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 防止spring内存溢出监听器 --><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener><!-- spring mvc servlet --><servlet> <description>spring mvc servlet</description> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring mvc 配置文件</description> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>

映射器 BeanNameUrlHandlerMapping默认表示将bean标签的name属性值当作url请求
适配器 SimpleControllerHandlerAdapter表示用于寻找实现了Controller接口、继承了AbstractCommandAcontroller(实例化的时候添加表单类)…实现类
试图解析器 InternalResourceViewResolver表示通过ModelAndView对象中封装的数据名找到真正的页面
转发需要转发的控制器

注解
要使用注解要先开启注解扫描,在springmvc配置文件中开启:

base-package:要扫描的包,类带有@Controller注解的包
在类上面加入@Controller注解,其实该类也就继承或者实现了Controller接口
基于注解的映射器(可写可不写)

在controller类中,如果想要向页面返回值,但是返回的不是ModelAndView,可以在该方法参数中加入(Model model)参数,该参数的存入的键值对,放在RequestScope作用域中。

在controller类中
方法中可以在方法中加入页面表单中传过来的参数,也可以对这些参数进行封装,封装在一个类中,把这个类当作参数在方法参数中,这样页面传过来的时候会进行自动封装。(在页面中不需要 对象名.属性名)
可以限定一个方法只在post请求或者get请求时被调用。@RequestMapping(value=”url” method=RequestMethod.POST) @RequestMapping(value=”url” method=RequestMethod.GET)
默认是:@RequestMapping(value=”url” method={RequestMethod.POST,RequestMethod.GET})

Ajax请求
在处理ajax请求的时候,spring使用了Jackson类库,可以将Java对像和json、xml数据进行互相转换。
可以将控制器返回的对象直接转换成json数据到客户端(要在返回找对象前加上注解@ResponseBody),客户端也可以将json数据传到服务器进行转换。
第一步:导包(Jackson):Jackson-core-asl-1.7.2jar Jackson-mapper-als-1.7.2jar
第二步:在spring配置文件中增肌:

 <bean class="org.springframework.web.servlet.mvc.annotation.annotationMethodHandlerAdapter">        <property name="cacheSeconds" value="0"></property>        <property name="messageConverters" >            <list>                <bean class="oeg.springframework.http.converter.json.MappingJacksonHttpMessageConve"></bean>            </list>        </property>    </bean>

第三步:在还返回的对象前加上注解@ResponseBody

文件上传
第一步:导包apache-commons-fileLoad.jar apache-commons-io.jar
第二步:在springmvc-servlet.xml配置文件中,增肌CommonsMultipartResoler配置

<bean class="org.springframework.web.multipart.commons.CommonsMultipartResoler">        <property name="defaultEncoding" value="GBK"></property> 默认编码是ISO-8859-1        <property name="maxInMemorySize"  value="10240"></property>最大内存大小        <property name="uoloadTempDir"  value="/upload"></property>上传后的目录名        <property name="maxUploadSize"  value="-1"></property>最大文件大小</bean>

第三步:在controller方法参数中的文件参数中加上@Requestparam(“file”)
列子:

  public String handleUploadData( String name, @Requestparam("file")  CommonsMultipartFile  file){        if(!file.isEmpty){            String path = this.servletContext.getRealpath(“/temp/”);            String fileName = file.getOriginalFilename();            String fileType = fileName .subString(fileName.lastIndexOf("."));            File newFile = new File(path,new Date().getTime()+fileType );            file.getFileItem().write(newFile);            return "success.jsp"        }else{            return "error.jsp";        }    }

在使用ServletContext的时候 类要实现ServletContextAware接口,并且设置set方法,就Ok了。

在xml文件中加入他dtd或者xsd引用后,在xml文件中无法显示
windows–>preference–>XMLN–>XML Catalog中加入引用即可

spring编码过滤器         <filter><filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
原创粉丝点击