springboot-pom.xml---------------------------------------------------------------------------------------------
<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.lzz</groupId>
<artifactId>springboot-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot devtools 依赖包. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<!-- servlet依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- 引入jstl的依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat的支持.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- 添加Spring boot的插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 热部署生效 -->
<fork>true</fork>
</configuration>
</plugin>
<!-- 资源文件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
pom.xml-----------------------------------------------------------------------------------------------------------
<dependencies>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>8080</port>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
springmvc.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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:config/*.properties" />
<!-- 配置注解驱动 :处理器适配器和映射器就不用在配置-->
<mvc:annotation-driven />
<!-- 配置包扫描器,扫描@Controller注解的类 -->
<context:component-scan base-package="obs.controller"/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置静态资源映射 -->
<mvc:default-servlet-handler/>
<!-- 配置multipart的解析器:文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<!-- <property name="maxUploadSize" value="5242880"/> -->
</bean>
</beans>
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/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>obs_manager</display-name>
<!-- 加载spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- 配置spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springMVC前端控制器_RESTful风格 -->
<servlet>
<servlet-name>obs_manager</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>obs_manager</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- post乱码过滤器 -->
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
login.jsp--------------------------------------------------------------------------------------------------------
<head>
<title>login</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" href="../css/table.css" rel="stylesheet"/>
<script src="../js/table.js"></script>
</head>
<body>
<div>
<form method="post" action="../login">
<table>
<tr>
<td></td>
<td><label>${msg}</label></td>
<tr>
<td>Username:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>Login:</td>
<td><input type="submit" value="Login"/></td>
</tr>
</table>
</form>
</div>
</body>
index.jsp------------------------------------------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Table</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" href="../css/table.css" rel="stylesheet"/>
<script type="text/javascript" src="../js/table.js"></script>
</head>
<body>
<table>
<th>
<tr><h3>OBS_Simple_Demo</h3></tr>
</th>
<tr>
<td>
<form method="post" enctype="multipart/form-data" action="../obs/upload">
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
</td>
<td>
<form method="get" action="../obs/search">
<input type="text" name="fileName" />
<input type="submit" value="查询"/>
</form>
</td>
</tr>
<tr></tr><tr></tr><tr></tr>
<tr>
<table class="altrowstable" id="alternatecolor">
<tr align="left">
<th width="300">名称</th>
<th width="150">存储类型</th>
<th width="150">大小</th>
<th width="250">修改时间</th>
<th width="150">区域</th>
<th>操作</th>
</tr>
<c:forEach items="${list }" var="file">
<tr>
<TD>${file.fileName }</TD>
<TD>${file.property }</TD>
<TD>${file.fileSize }</TD>
<TD>${file.updatetime }</TD>
<TD>${file.category }</TD>
<TD>
<a href="../obs/download/${file.fileName }">下载</a>
</TD>
</tr>
</c:forEach>
</table>
</table>
</body>
</html>
table.css------------------------------------------------------------------------------------------------------------------
table.altrowstable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #a9c6c9;
border-collapse: collapse;
}
table.altrowstable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
background-color: #E1E4E5;
}
table.altrowstable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
td {
height: 30px;
white-space: nowrap;
}
.evenrowcolor {
background-color:#F7F8F8;
}
div {
border-radius: 20px;
width: 300px;
height: 350px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
table.js------------------------------------------------------------------------------------------------------------
function altRows(id) {
if (document.getElementsByTagName) {
var table = document.getElementById(id);
var rows = table.getElementsByTagName("tr");
for (var i = 0; i < rows.length; i++) {
if (i % 2 == 0) {
rows[i].className = "evenrowcolor";
}
}
}
}
window.onload = function () {
altRows('alternatecolor');
}
LoginController---------------------------------------------------------------------------------------------------------
@Controller
public class LoginController {
@Autowired
private ObsService obsService;
@RequestMapping("/")
public String login() {
return "login";
}
@RequestMapping(value="/login", method=RequestMethod.POST)
public String index(String username, String password, Model model) {
if (!username.equals("zhangsan")) {
model.addAttribute("msg", "username error");
return null;
}
if (!password.equals("123")) {
model.addAttribute("msg", "password error");
return null;
}
model.addAttribute("list", obsService.getFileList());
return "index";
}
}
ObsController--------------------------------------------------------------------------------------------------------------
@Controller
@RequestMapping("/obs")
public class ObsController {
@Autowired
private ObsService obsService;
@RequestMapping(value="/upload", method=RequestMethod.POST)
public String uploadUserPic(@RequestParam("file") MultipartFile file, Model model) {
//获取原始名称
String originalFilename = file.getOriginalFilename();
if (file != null && originalFilename != null && originalFilename.length()>0) {
System.out.println("file name is null-------------");
model.addAttribute("msg", "文件格式错误");
return "index";
}
System.out.println("file name is : " + originalFilename);
//生成新的名称
String newFilename = "new file" + originalFilename.substring(originalFilename.lastIndexOf("."));
//创建新的图片文件
File newFile = new File("D:/temp/" + newFilename);
//将内存中的图片数据写入磁盘(即上传到服务器端)
try {
file.transferTo(newFile);
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
System.out.println("upload success ----------------");
return "index";
}
@RequestMapping(value="/search", method=RequestMethod.GET)
public String search(@RequestParam String fileName, Model model) {
if (fileName == null || fileName.length() == 0) {
model.addAttribute("list", obsService.getFileList());
return "index";
}
if (fileName.equals("testName1")) {
model.addAttribute("list", obsService.getFile());
return "index";
}
return "index";
}
}
TestFile-------------------------------------------------------------------------------------------------------------
private String fileName;
private String fileSize;
private String updatetime;
private String category;
private String property;
ObsService---------------------------------------------------------------------------------------------------------
@Service
public class ObsService {
public List<TestFile> getFileList() {
List<TestFile> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
TestFile file = new TestFile();
file.setFileName("testName" + i);
file.setFileSize("2" + i);
file.setUpdatetime("2012-12-12");
file.setCategory("cn-earth-2");
file.setProperty("可读写");
list.add(file);
}
return list;
}
public List<TestFile> getFile() {
List<TestFile> list = new ArrayList<>();
TestFile file = new TestFile();
file.setFileName("testName1");
file.setFileSize("222");
file.setProperty("haha");
file.setUpdatetime("2012-12-12");
file.setCategory("789789");
list.add(file);
return list;
}
}