SSM+Maven商城项目——2 页面展示

1. 记得将所有的项目标记成对应的文件类型

2.将前端所有资源引入到backend中的WEB-INF下

在这里插入图片描述
并且创立views这个文件夹,主要用来放html的。

3. 配置web.xml

首先要先替换的maven自动的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">
	<!--配置内容-->
</web-app>

4. 配置SpringMVC的前端控制器、编码过滤器


<!--配置springmvc前端控制器-->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>    <!--让servlet的init方法提前-->
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--配置过滤器让其无法乱码-->
  <filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

5. 创立资源文件夹resource,用于放置spring相关配置,如上图所示。

记得要与java文件夹同级。
创立spring-dao.xml spring-service.xml spring-mvc.xml

6.配置spring-mvc.xml配置文件

必须要配置的有视图解析器(为了方便)、注解驱动(可以扫描注解)、组件扫描(扫描控制器)

<!--开启注解驱动-->
    <mvc:annotation-driven/>

    <!-- 视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 扫描我的控制器-->
    <context:component-scan base-package="com.itrucheng.zshop.backend.controller"></context:component-scan>

注意:在上面的xml中,mvc、context的依赖网址是:

  xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"

貌似有关的都是http://www.springframework.org/schema下的

发现原来还可以在此配置文件下配置一个临时的控制器:

<!-- 在xml中临时定义一个控制器-->
	<!--path是路径,view-name是视图的jsp名字-->
    <mvc:view-controller path="/showLogin" view-name="login"></mvc:view-controller>

然后在index.jsp中加入jsp指令,使得能够直接在index.jsp中转发到login.jsp中

<%@ page  contentType="text/html; UTF-8" %>

<html>
<body>
<h2>Hello World!</h2>

<jsp:forward page="${pageContext.request.contextPath}/showLogin"></jsp:forward>

</body>
</html>

7. 将所有前端中的链接地址修改,并配合spring-mvc.xml中的资源路径映射器

首先将所有的…/ 还是…/什么的都换成${pageContext.request.contextPath}/
在这里插入图片描述
然后再到spring-mvc.xml中配置资源路径映射器

  <!-- 配置资源路径映射器-->
    <mvc:resources mapping="/css/**" location="/WEB-INF/css/"></mvc:resources>
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/"></mvc:resources>
    <mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/"></mvc:resources>
    <mvc:resources mapping="/iconfont/**" location="/WEB-INF/iconfont/"></mvc:resources>
    <mvc:resources mapping="/images/**" location="/WEB-INF/images/"></mvc:resources>

这样,凡是碰到/css/… 什么的,都会被替换到/WEB-INF/css/…,但在浏览器中是无法看到这种映射的。

8.写下控制器

现在只是为了看到前台效果,所以可以将控制器写的简单点:

package com.itrucheng.zshop.backend.controller;/*
 * auth:熊汝成
 * date:2019/11/22
 * description:
 */

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/backend/sysuser")
public class SysuserController {

    @RequestMapping("/login")
    public String login() {
        //实现登录的功能
        return "main";
    }

}

9.修改backend的pom.xml,使得能够让maven扫描所有的资源文件!!!

今天因为这个系统报错:
Could not resolve bean definition resource pattern
我的所有的resource下的所有xml无法发布出去。
通过网上找到其解决方案,就是maven只会自动扫描java文件,但不会扫描xml等文件,所有需要通过pom中配置一下:
在build中添加:

<resources>
         <resource>
         <directory>src/main/java</directory>
         <includes>
             <include>**/*.properties</include>
             <include>**/*.xml</include>
         </includes>
         <filtering>false</filtering>
         </resource>

         <resource>
             <directory>src/main/resource</directory>
             <includes>
                 <include>**/*.properties</include>
                 <include>**/*.xml</include>
             </includes>
             <filtering>false</filtering>
         </resource>
     </resources>

意思是让maven能够将所有的xml properties文件扫描。这样这些文件发布后就跑到classes中去了。
在这里插入图片描述
需要注意的是:
1. 和java文件夹同级编写的resource文件夹下的资源默认被发布到了classes文件夹下,这也是为什么web.xml中可以写classpath:spring-*。
2. 虽然java被编译放到了项目/WEB-INF/classes下,但是我们在用控制器路径的时候还是可以用${pageContext.request.contextPath}/action,前面的 ${…}是表示项目的名称也就是项目的根目录,不用跑到classes文件下进行映射。

10.最后为了方便,可以自定义指令:

在idea中配置指令,clean install tomcat7:run

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值