问题描述:
写了个freemarker的web小demo,启动时无论访问静态文件还是访问controller都报404。
原因排查:
首先,认为可能是spring-mvc.xml配置文件不正确,可是检查了一遍,用了最简配置后依然报404,此原因排除:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/tool
http://www.springframework.org/schema/tool/spring-tool.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/p
http://www.springframework.org/schema/p/spring-p.xsd">
<!--开启定时注解功能-->
<task:annotation-driven/>
<!-- 自动扫描组件
注意扫描的包结构建议配置在controller层,因为如果配置在其它层可能出现事务失效
-->
<context:component-scan base-package="com.jessica.freemarker.controller"/>
<!-- 配置注解驱动 -->
<mvc:annotation-driven/>
<!--開啓aop,對類代理-->
<aop:config proxy-target-class="true"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<mvc:default-servlet-handler/>
<!--配置靜態資源-->
<!--<mvc:resources mapping="/js/**" location="/js/"/>-->
<!--<mvc:resources mapping="/metronic/**" location="/metronic/"/>-->
<!-- 配置网络资源视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp"/>
</beans>
然后,我认为可能是web.xml配置的原因,于是把spring的配置统统注释,把项目变成一个普通web,然而访问静态文件依然404;
接着查看pom.xml文件,有一个插件关于打包的配置,本着一切从简的思路,注释掉:
<build>
<finalName>freemarker-demo</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<!--<resources>
<resource>
<targetPath>${project.build.directory}/classes</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.ttf</include>
</includes>
</resource>
<resource>
<targetPath>${project.build.directory}/classes</targetPath>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>-->
</build>
然而问题还是没有解决,有点郁闷了;
最后回归到tomcat上(现在才想到,愚蠢的大脑啊),之前的项目都可以正常启动,所以排除通用配置,点开Run Configuration配置:
红框选中的这里,在我选择要部署的war文件后的原始地址是http://localhost:8080/freemarker_demo_war_exploded,我刚开始认为这里的路径是可以自定义的,然而和另一个地方的配置也有关:
点开上方第二个选项卡Deployment:
发现这里有个应用地址的配置,原来如此,改为“/”,再把之前的浏览器访问URL改为"http://localhost:8080/",再次运行项目,OK。