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"})
@EnableWebMvc
public 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>
本文介绍了如何在Spring MVC项目中实现无XML配置,通过在Java类上使用@Configuration、@ComponentScan、@EnableWebMvc注解,以及继承WebMvcConfigurerAdapter进行配置。同时,文章提到了MVCLoader类的实现,用于初始化DispatcherServlet,并展示了ExplorerController类的编写,用于控制器功能。此外,还讲解了使用easyui创建界面展示目录树结构的过程。
1202

被折叠的 条评论
为什么被折叠?



