资源在WEB-INF目录下

动态资源放在WEB-INF目录下,不可对外进行访问

  • 1.你项目的安全性会更高,别人只能通过请求转发才能进入web-inf进入到资源文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.chentianyu</groupId>
  6. <artifactId>resourcesInWebInf_Learn_One</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <maven.compiler.source>1.8</maven.compiler.source>
  12. <maven.compiler.target>1.8</maven.compiler.target>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <version>4.11</version>
  19. <scope>test</scope>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework</groupId>
  23. <artifactId>spring-webmvc</artifactId>
  24. <version>5.3.16</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>javax.servlet</groupId>
  28. <artifactId>javax.servlet-api</artifactId>
  29. <version>4.0.1</version>
  30. </dependency>
  31. </dependencies>
  32. <build>
  33. <resources>
  34. <resource>
  35. <directory>src/main/java</directory>
  36. <includes>
  37. <include>**/*.xml</include>
  38. <include>**/*.properties</include>
  39. </includes>
  40. </resource>
  41. <resource>
  42. <directory>src/main/resources</directory>
  43. <includes>
  44. <include>**/*.xml</include>
  45. <include>**/*.properties</include>
  46. </includes>
  47. </resource>
  48. </resources>
  49. </build>
  50. </project>

一系列的main.jsp和index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>webapp-index页面</title>
</head>
<body>
<p>Hello!</p>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>admin-index</title>
</head>
<body>
  <p>这是admin目录下的index</p>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>admin-main</title>
</head>
<body>
    <p>main...</p>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>web-inf里的jsp-index</title>
</head>
<body>
    <h2>web-inf里的jsp-index</h2>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>web-inf里的jsp-main</title>
</head>
<body>
    <h2>web-inf里的jsp-main</h2>
</body>
</html>

WEB-INF目录下的动态资源,不可直接访问,只能通过请求转发的方式进行访问

修改springmvc.xml文件中的前缀

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>
package com.chentianyu.controller;

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

@Controller
public class WebInfAction {
    @RequestMapping("/showIndex")
    public String showIndex(){
        System.out.println("访问了index.jsp");
        return "index";
    }
    @RequestMapping("/showMain")
    public String showMain(){
        System.out.println("访问了main.jsp");
        return "main";
    }
}

现在就可以管了,这样可以在**action**类中添加判断才可以通过

运行tomcat访问http://localhost:8080/showIndex