码迷,mamicode.com
首页 > 编程语言 > 详细

第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第五天】

时间:2019-10-14 10:23:28      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:www   load   parent   对话框   工程   通配符   ctr   character   静态资源   

https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040

第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】

第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第五天】

 

开发环境:

Eclipse IDE for Enterprise Java Developers
OS: Windows 10, v.10.0, x86_64 / win32
Java version: 1.8.0_221

05.第五天(前台工程搭建、首页商品类目显示)

技术图片

 


taotao-rest项目

web.xml

    <!-- springmvc的前端控制器 -->
    <servlet>
        <servlet-name>taotao-rest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, 
        springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
        <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>taotao-rest</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

因为springMVC在web.xml里配置的前端控制器的url-pattern拦截的不是:/

所以在开发时resources目录下的springmvc.xml里就不需要配置静态资源的映射,因为这些静态资源在浏览器发送请求时没有被拦截。

技术图片


 03.服务层工程搭建

 一、跳过测试的Maven命令:install -DskipTests

技术图片

install -DskipTests

 

二、 放置静态资源的目录是 webapp目录

技术图片

taotao-portal项目模块

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="WebApp_ID" version="2.5">
    <display-name>taotao-portal-web</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>
        <!-- <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>


    <!-- springmvc的前端控制器 -->
    <servlet>
        <servlet-name>taotao-portal</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, 
        springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
        <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>taotao-portal</servlet-name>
        <!-- 伪静态化给搜索引擎看的 -->
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>
View Code

 


5  商品分类展示

首页左侧有一个商品分类。当鼠标分类上,需要展示出此分类下的子分类。

当鼠标滑动到连接上触发mousemove事件。页面做一个ajax请求,请求json数据包含分类信息,得到json数据后初始化分类菜单,展示。

 Eclipse快捷键:Ctrl+L,这样直接弹出Go to line对话框,在里面输入要跳转到的行号,按回车或者单击‘确定’。

或者找到Eclipse界面下的状态栏,找到行号列,双击它,也会弹出Go to line对话框,也是输入行号,然后按回车键。

技术图片

然后在webapp目录下copy一个category.json

技术图片

使用JSON Viewer可以Format这个json然后方便查看分析其结构组成。

技术图片

 第一层:u、n(包含a标签)、i

第二层:u、n、i

第三层:字符串

使用ajax访问本工程的json数据(jQuery的方式)

技术图片

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

四、  Ajax跨域请求:出于安全考虑,js设计时不可以跨域。

 什么是跨域:

1、域名不同时。

2、域名相同,端口不同。

 

只有域名相同、端口相同时,才可以访问。

 

可以使用jsonp解决跨域问题。

Jsonp其实就是一个跨域解决方案。Js跨域请求数据是不可以的,但是js跨域请求js脚本是可以的。可以把数据封装成一个js语句,做一个方法的调用。跨域请求js脚本可以得到此脚本。得到js脚本之后会立即执行。

可以把数据做为参数传递到方法中。就可以获得数据。从而解决跨域问题。

技术图片

 

 

五、在POJO中的成员变量上,使用@JsonProperty注解

技术图片

 

 

end

第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第五天】

标签:www   load   parent   对话框   工程   通配符   ctr   character   静态资源   

原文地址:https://www.cnblogs.com/MarlonKang/p/11453771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!