e3mall项目:首页(前台)
e3mall项目:首页
一、子模块创建
(1)pom.xml中打包方式为war
(2)包结构如下:
二、相关配置文件
(1)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:dubbo="http://code.alibabatech.com/schema/dubbo" 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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.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"> <!--读取resource配置文件--> <context:property-placeholder location="classpath:other/resource.properties"/> <!--开启注解支持--> <context:component-scan base-package="cn.e3mall.portal.controller" /> <!--配置SpringMVC注解支持--> <mvc:annotation-driven /> <!--配置SpringMVC视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 引用dubbo服务 --> <dubbo:application name="e3-portal-web"/> <dubbo:registry protocol="zookeeper" address="192.168.25.130:2181"/> <dubbo:reference interface="cn.e3mall.content.service.ContentService" id="contentService" /> </beans>
(2)resource.properties
#首页轮播图对应的categoryid CONTENT_AD1_CATEGORYID=89 CONTENT_AD2_CATEGORYID=96
(3)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>e3-p-web</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <!--配置SpringMVC前段控制器--> <servlet> <servlet-name>e3-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>e3-manager</servlet-name> <!--伪静态化--> <url-pattern>*.html</url-pattern> </servlet-mapping> <!--POST提交乱码解决--> <filter> <filter-name>encoding</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>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
(4)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"> <parent> <artifactId>e3-parent</artifactId> <groupId>cn.e3mall</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>e3-portal-web</artifactId> <packaging>war</packaging> <name>e3-portal-web</name> <dependencies> <dependency> <groupId>cn.e3mall</groupId> <artifactId>e3-manager-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>cn.e3mall</groupId> <artifactId>e3-content-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>cn.e3mall</groupId> <artifactId>e3-manager-dao</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <!-- JSP相关 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8082</port> <path>/</path> </configuration> </plugin> </plugins> </build> </project>
三、代码实现
(1)PageController
package cn.e3mall.portal.controller; import cn.e3mall.content.service.ContentService; import cn.e3mall.entity.TbContent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; /** * 页面跳转控制层 * Author: xushuai * Date: 2018/5/17 * Time: 10:07 * Description: */ @Controller public class PageController { @Value("${CONTENT_AD1_CATEGORYID}") private Long CONTENT_AD1_CATEGORYID; @Value("${CONTENT_AD2_CATEGORYID}") private Long CONTENT_AD2_CATEGORYID; @Autowired private ContentService contentService; /* * 前往主页 */ @RequestMapping("/index.html") public String toHome(Model model){ //使用内容分类id查询轮播图信息 List<TbContent> ad1ContentList = contentService.getContentList(CONTENT_AD1_CATEGORYID); List<TbContent> ad2ContentList = contentService.getContentList(CONTENT_AD2_CATEGORYID); //将内容列表保存并发送到页面 model.addAttribute("ad1List",ad1ContentList); model.addAttribute("ad2List",ad2ContentList); return "index"; } }
(2)ContentService、ContentServiceImpl(新增方法及其实现)
/** * 按内容分类id查询内容列表 * @auther: xushuai * @date: 2018/5/21 16:46 */ List<TbContent> getContentList(Long categoryId);
@Override public List<TbContent> getContentList(Long categoryId) { //创建查询条件对象 TbContentExample example = new TbContentExample(); //封装查询条件 example.createCriteria().andCategoryIdEqualTo(categoryId); //执行查询获取结果 List<TbContent> tbContents = contentMapper.selectByExampleWithBLOBs(example); return tbContents; }