Maven结构
项目搭建常规操作
页面的完成
前后端合作
完成图片上传
Maven结构
可以帮我们自动导包,自动编译,规范代码
- src/main/java ——> Java代码的存放位置
- src/main/resource ——> 资源文件的存放位置
- src/test/java ——> 测试代码的存放位置
- src/main/resource——> 测试代码资源文件的存放位置
项目搭建的常规操作
- 在数据库建对应的表 如(t_image)
- 三层架构
2.1 仓库层(dao)
2.2 业务层(service)
2.3 控制层(controller) - 资源文件的配置
3.1 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 id="/test" class="cn.itsource.springtest.SpringTest"></bean> -->
<!-- 打描包:支持对应的注解,变成bean -->
<context:component-scan base-package="cn.itsource.cms.imageservice,cn.itsource.cms.imagecontroller,cn.itsource.cms.imagedao" />
<!-- 读取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:jDBC的模板(公共的代码已经完成)
-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
3.2 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" />
<!--支持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.3 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>springcrud</display-name>
<!-- 解决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>
<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>
<!--
Spring自己准备了一个监听器,可以把自己启动起来:ContextLoaderListener
Tomcat一启动,Spring就会启动 -> 就去找它的配置文件 -> /WEB-INF/applicationContext.xml
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<!-- 代表配置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>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置核心控制器的映射 -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
页面的完成
凡是在WEB-INF中的页面都要通过Controller访问
例如: SystemController —> 进入后台的主页面
ImagesController—>进入CRUD管理界面
前后端合作
4.1 前端会把html做好,然后我们把他做的页面搞到我们的项目中来(图片,js,css的引入路径都要进行修改)
4.2 iframe标签 -> 可以把其它的页面直接放入到页面中来
4.3 注意点:如果咱们配置有上下文路径:所有跳转的路径前:${pageContext.request.contextPath }
完成图片上传
5.1 form标签中需要配置 method=“post” enctype=“multipart/form-data”
5.2 后台要根据上传的文件名进行接收
①.修改名称 ②.获取路径 ③.保存图片
④.保存Images对象(绝对路径的地址,名称)