spring mvc无xml
来源:互联网 发布:ae cc 2015 中文 mac 编辑:程序博客网 时间:2024/05/18 03:44
Spring MVC无xml文件
项目结构
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xsh136</groupId> <artifactId>explorer</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!--引入servlet--> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--引入spring mvc--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.9.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.8</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </build></project>
配置
WEBConf.java
配置文件,继承 WebMvcConfigurerAdapter类,功能与
springmvc-config.xml
配置文件一样,类上应该加@Configuration、
@ComponentScan、
@EnableWebMvc三个注解,重写的方法应该加上@Bean注解
package com.xsh136.explorer.conf;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;import org.springframework.web.multipart.MultipartResolver;import org.springframework.web.multipart.commons.CommonsMultipartResolver;import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import org.springframework.web.servlet.view.InternalResourceViewResolver;import org.springframework.web.servlet.view.JstlView;/** * Created by ZWW on 2017/8/3. */@Configuration@ComponentScan(basePackages = {"com.xsh136.explorer.controller"})@EnableWebMvcpublic class WEBConf extends WebMvcConfigurerAdapter{ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { super.configureDefaultServletHandling(configurer); configurer.enable(); } @Bean public InternalResourceViewResolver viewResolver(){ InternalResourceViewResolver bean=new InternalResourceViewResolver(); bean.setViewClass(JstlView.class); bean.setPrefix("/WEB-INF/jsp/"); bean.setSuffix(".jsp"); return bean; } @Bean("multipartResolver") public MultipartResolver multipartResolver(){ CommonsMultipartResolver bean=new CommonsMultipartResolver(); bean.setDefaultEncoding("utf-8"); return bean; } @Bean public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){ MappingJackson2HttpMessageConverter bean=new MappingJackson2HttpMessageConverter(); return bean; }}
MVCLoader类继承AbstractAnnotationConfigDispatcherServletInitializer类,实现其中的方法,修改getServletConfigClasses()方法和getServletMappings()方法
package com.xsh136.explorer.conf;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;/** * Created by ZWW on 2017/8/3. */public class MVCLoader extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[0]; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{WEBConf.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; }}
编写ExplorerController类,加@Controller、
@RequestMapping注解,代码如下:
package com.xsh136.explorer.controller;import com.xsh136.explorer.tree.TreeNodeMy;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import javax.swing.filechooser.FileSystemView;import java.io.File;import java.io.FileFilter;import java.text.DecimalFormat;import java.text.SimpleDateFormat;import java.util.*;/** * Created by ZWW on 2017/8/3. */@Controller@RequestMapping("/explorer")public class ExplorerController { FileSystemView fsv=FileSystemView.getFileSystemView(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm"); DecimalFormat df=new DecimalFormat("###,####"); @RequestMapping("/hello") public String hello(){ return "hello"; } @RequestMapping("/tree") @ResponseBody public List<TreeNodeMy> tree(String id){ List<TreeNodeMy> treeNodeMies =new ArrayList<>(); File[] files=null; if(id==null){ files=File.listRoots(); }else { files=new File(id).listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory(); } }); } if(files!=null){ for(File file:files){ treeNodeMies.add(new TreeNodeMy(file.getAbsolutePath(),fsv.getSystemDisplayName(file))); } } return treeNodeMies; } @RequestMapping("/list") @ResponseBody public List<Map<String,String>> list(String id){ List<Map<String,String>> datas=new ArrayList<>(); File[] files=null; if(id!=null){ files=new File(id).listFiles(); } if(files!=null){ for(File file:files){ Map<String,String> map=new HashMap<>(); map.put("name",fsv.getSystemDisplayName(file)); map.put("modifyDate",sdf.format(new Date(file.lastModified()))); map.put("type",fsv.getSystemTypeDescription(file)); String size=""; if(file.isFile()){ size=df.format(file.length()); map.put("size",size+"B"); } datas.add(map); } } return datas; }}
编写TreeNodeMy类,用于显示目录树结构
package com.xsh136.explorer.tree;/** * Created by ZWW on 2017/8/3. */public class TreeNodeMy { private String id; private String text; private String state="closed"; public TreeNodeMy(String id, String text) { this.id = id; this.text = text; } public TreeNodeMy() { } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getState() { return state; } public void setState(String state) { this.state = state; }}
采用easyui来进行界面显示,需要导入响应的文件,在WEB-INF下建立一个static文件夹,将所需要的文件加入进去
然后在webapp下建立一个html文件,代码如下
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" type="text/css" href="static/themes/icon.css"> <link rel="stylesheet" type="text/css" href="static/themes/default/easyui.css"> <script type="text/javascript" src="static/jquery.min.js"></script> <script type="text/javascript" src="static/jquery.easyui.min.js"></script> <script type="text/javascript"> $(function () { $('#tree').tree({ url:'explorer/tree', onClick: function(a){ // alert(a.text); // 在用户点击的时候提示 $("#list").datagrid({ queryParams:{ id:a.id } }) } }); $('#list').datagrid({ url:'explorer/list', columns:[[ {field:'name',title:'名称',width:100}, {field:'modifyDate',title:'修改日期',width:100}, {field:'type',title:'类型',width:100}, {field:'size',title:'大小'} ]] }); }) </script></head><body class="easyui-layout"> <div data-options="region:'west',title:'目录树',iconCls:'icon-reload', split:true" style="width:300px;"> <ul id="tree"> </ul> </div> <div data-options="region:'center',title:'文件列表'" style="padding:5px;"> <table id="list"></table> </div></body></html>
阅读全文
0 0
- spring mvc无xml
- Spring MVC 无XML配置入门示例
- maven+spring mvc环境搭建xml版(无web.xml,maven jetty插件运行)
- Spring mvc web.xml
- spring-mvc.xml
- spring-mvc.xml
- jeesite spring-mvc.xml
- spring mvc web.xml
- spring-mvc.xml
- spring mvc --xml
- spring-mvc.xml参考
- spring-mvc.xml配置
- spring MVC HelloWord(xml)
- spring-mvc.xml配置
- spring mvc xml配置
- spring-mvc.xml
- spring-mvc.xml
- Spring-mvc.xml
- Java 异常Exception e中e的getMessage()和toString()方法的区别
- 字母大小写问题造成的Test Suit的Library中添加Selenium2Library显示红色
- 《概率论与数理统计_浙江大学》_第一章_概率论基本概念
- C++顺序容器类:矢量类、列表类和双端队列类
- leetcode 166. Fraction to Recurring Decimal
- spring mvc无xml
- 移动端学习笔记(二)
- 使用pycharm运行scrapy项目
- kettle连接多个数据库
- Android Sudio每次启动很慢的解决办法
- 9.8.2 删除那些不需要的文件
- UESTC-621 吴神的大脑
- Ubuntu修改计算机名
- Unity3D之虚拟摇杆