天天看点

框架 day77 涛涛商城项目-前台系统及门户搭建,JSONP解决跨域ajax请求1  前台系统搭建2  服务层工程搭建

淘淘商城第五天

                       讲师:入云龙

1  前台系统搭建

前台系统就是淘淘商城。

前台系统和后台系统是分开的,只在数据库层面有关系。都是同一个数据库。

淘淘商城首页:

框架 day77 涛涛商城项目-前台系统及门户搭建,JSONP解决跨域ajax请求1  前台系统搭建2  服务层工程搭建

原架构

框架 day77 涛涛商城项目-前台系统及门户搭建,JSONP解决跨域ajax请求1  前台系统搭建2  服务层工程搭建

优化后的架构:

框架 day77 涛涛商城项目-前台系统及门户搭建,JSONP解决跨域ajax请求1  前台系统搭建2  服务层工程搭建

优点:

1、前台系统和服务层可以分开,降低系统的耦合度。

2、开发团队可以分开,提高开发效率

3、系统分开可以灵活的进行分布式部署。

缺点:服务之间通信使用接口通信,开发工作量提高。

前台系统分为两部分,一部分是服务层web工程,功能就是发布服务

另外一部分:表现层,展示页面,没有业务逻辑。所有业务逻辑就是调用服务层的服务。

2  服务层工程搭建

使用maven创建一个war工程。

2.1    创建工程

框架 day77 涛涛商城项目-前台系统及门户搭建,JSONP解决跨域ajax请求1  前台系统搭建2  服务层工程搭建

2.2    使用到的技术

1、Mybatis

2、Spring

3、Springmvc

2.3    配置工程

2.3.1  Pom文件

<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>

     <parent>

          <groupId>com.taotao</groupId>

          <artifactId>taotao-parent</artifactId>

          <version>0.0.1-SNAPSHOT</version>

     </parent>

     <groupId>com.taotao</groupId>

     <artifactId>taotao-rest</artifactId>

     <version>0.0.1-SNAPSHOT</version>

     <packaging>war</packaging>

     <dependencies>

          <dependency>

               <groupId>com.taotao</groupId>

               <artifactId>taotao-manager-mapper</artifactId>

               <version>0.0.1-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>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>

</project>

2.3.2  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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

     id="taotao" version="2.5">

     <display-name>taotao-manager</display-name>

     <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>

     <!-- 加载spring容器 -->

     <context-param>

          <param-name>contextConfigLocation</param-name>

          <param-value>classpath:spring/applicationContext-*.xml</param-value>

     </context-param>

     <listener>

          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

     </listener>

     <!-- 解决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>

     private List<?> getCatList(long parentId) {

          //创建查询条件

          TbItemCatExample example = new TbItemCatExample();

          Criteria criteria = example.createCriteria();

          criteria.andParentIdEqualTo(parentId);

          //执行查询

          List<TbItemCat> list = itemCatMapper.selectByExample(example);

          //返回值list

          List resultList = new ArrayList<>();

          //向list中添加节点

          for (TbItemCat tbItemCat : list) {

               //判断是否为父节点

               if (tbItemCat.getIsParent()) {

                    CatNode catNode = new CatNode();

                    if (parentId == 0) {

                         catNode.setName("<a href='/products/"+tbItemCat.getId()+".html'>"+tbItemCat.getName()+"</a>");

                    } else {

                         catNode.setName(tbItemCat.getName());

                    }

                    catNode.setUrl("/products/"+tbItemCat.getId()+".html");

                    catNode.setItem(getCatList(tbItemCat.getId()));

                    resultList.add(catNode);

               //如果是叶子节点

               } else {

                    resultList.add("/products/"+tbItemCat.getId()+".html|" + tbItemCat.getName());

               }

          }

          return resultList;

     }

}

4.5    Controller

接收页面传递过来的参数。参数就是方法的名称。返回一个json数据,需要把json数据包装成一句js代码。返回一个字符串。

参数:回调方法名称

返回值:字符串

@Controller

public class ItemCatController {

     @Autowired

     private ItemCatService itemCatService;

     @RequestMapping(value="/itemcat/list",

               produces=MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")

     @ResponseBody

     public String getItemCatList(String callback) {

          CatResult catResult = itemCatService.getItemCatList();

          //把pojo转换成字符串

          String json = JsonUtils.objectToJson(catResult);

          //拼装返回值

          String result = callback + "(" + json + ");";

          return result;

     }

}

方法二:

@RequestMapping("/itemcat/list")

     @ResponseBody

     public Object getItemCatList(String callback) {

          CatResult catResult = itemCatService.getItemCatList();

          MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(catResult);

          mappingJacksonValue.setJsonpFunction(callback);

          return mappingJacksonValue;

     }

继续阅读