资源在WEB-INF目录下
把动态资源放在WEB-INF目录下,不可对外进行访问
- 1.你项目的安全性会更高,别人只能通过请求转发才能进入
web-inf进入到资源文件
<?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"><modelVersion>4.0.0</modelVersion><groupId>com.chentianyu</groupId><artifactId>resourcesInWebInf_Learn_One</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.16</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version></dependency></dependencies><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes></resource><resource><directory>src/main/resources</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes></resource></resources></build></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
