CMS系统
第一天学习
CMS:内容(文章)管理系统
它类似的项目CMS管理系统:新闻发布系统、博客blog管理系统、QQ空间
.1. Maven:结构 它可以帮我们自动导包,自动编译,规范代码,…;
- src/main/java -> Java代码的存放位置
src/main/resources -> 资源文件的存放位置
src/test/java -> 测试代码的位置
src/test/resources -> 代码资源文件的位置
. 2.项目搭建的常规操作
- 数据库建表:t_image
建三层架构:dao(持久层),service(业务层),controller(控制层)
spring和springMVC环境配置:
(1)导包
(2)配置文件
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>cms</display-name>
<!-- Spring的核心配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 核心控制器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- SpringMVC的配置文件的位置 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<!--记SpringMVC跟着服务器(tomcat)的启动而启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!--使用杠更加符合咱们的RESTful风格 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 监听服务器(tomcat)的启动,让Spring也同启动 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置相应的过滤器:角色SpringMVC 的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>
</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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
" >
<!-- 打描包:支持对应的注解,变成bean -->
<context:component-scan base-package="cn.itsource.cms.service.impl,cn.itsource.cms.dao.impl" />
<!-- 读取jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置dataSource(连接池) -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--
专门为咱们准备了一个类,用来完成数据库的操作:JdbcTemplate
JdbcTemplate:jDBC的模板(公共的代码已经完成)
-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
applicationContext-mvc.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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
" >
<!-- 引入其它的Spring配置文件
<import resource="classpath:applicationContext.xml"/>
-->
<!-- 扫描包 -->
<context:component-scan base-package="cn.itsource.cms.web.controller" />
<!--支持SpringMVC特有的注解 -->
<mvc:annotation-driven />
<!-- 对静态资源放行 -->
<mvc:default-servlet-handler />
<!-- 视图解析器:自动为咱们添加前缀与后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 上传解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>2000000000</value>
</property>
</bean>
</beans>
.3.搞定页面(凡是WEB-INF中的页面都要通过Controller访问)
SystemController -> 进入后台的主页面
package cn.itsource.cms.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/system")
public class SystemController {
@RequestMapping("/index")//配置访问路径
public String index(){
return "index";
}
}
ImagesController -> 进入轮播图(CRUD)管理页面
package cn.itsource.cms.web.controller;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import cn.itsource.cms.domain.Images;
import cn.itsource.cms.service.IImagesService;
@Controller//Controller 就相当于你的bean已经配置好了
@RequestMapping("/images")
public class ImagesController {
@Autowired//自动注入
private IImagesService imageService;
@RequestMapping("/query")
public String query(Model model){//使用Model传参
//绑定参数
model.addAttribute("imageList",imageService.findAll());
return "main";
}
/**
* 添加或者修改的跳转
* @return
*/
@RequestMapping("/input")
public String input(){
return "main_add";
}
/**
* 添加或者修改功能
* @return
* @throws IOException
* @throws IllegalStateException
*/
@RequestMapping("/save")
public String save(Images images,HttpServletRequest req) throws Exception{
//一.解决上传的名称问题
//1.拿到相应的文件
MultipartFile fileImg = images.getFileImg();
//2.拿到文件名称
String fileName = fileImg.getOriginalFilename();
//3.拿到文件的扩展名
String extName = FilenameUtils.getExtension(fileName);
//4.获取随机名称
String uuid = UUID.randomUUID().toString();
//5.拼接一个新的名称
String newFileName = uuid+"."+extName;
//二.解决上传的路径问题
//2.1获取真实路径
String realPath = req.getServletContext().getRealPath("/upload");
//2.2创建文件
File file = new File(realPath,newFileName);
//2.3创建父文件路径(装file的文件夹路径)
File parentFile = file.getParentFile();
if(!parentFile.exists()){//判断有没有文件夹,没有就创建
parentFile.mkdirs();
}
//三.保存文件 transferTo:保存文件的方法
fileImg.transferTo(file);
//添加数据
//1.添加名称
images.setStorename(fileName);
//2.添加地址
images.setStorepath("/upload/"+newFileName);
imageService.save(images);
return "redirect:/images/query";
}
}
.4. 和前端合作
-
(1)前端会把html做好,然后我们把他做的页面搞到我们的项目中来(图片,js,css的引入路径都要进行修改)
-
(2) iframe -> 可以把其它的页面直接放入到页面中来(src="…")
-
<iframe scrolling="no" id="mainContent" name="mainContent" src="/image/query" frameborder="0" src="" style="min-height:600px;width:100%;height:100%;"></iframe>
-
(3)注意点:如果咱们配置有上下文路径:所有跳转的路径前加:${pageContext.request.contextPath }
.5.完成图片上传 -
(1)form中需要配置 method=“post” enctype=“multipart/form-data”
-
(2)后台要根据上传的文件名进行接收
①.修改名称 ②.获取路径 ③.保存图片(fileImg.transferTo(file))
④.保存Images对象(绝对路径的地址,名称)