Java HTTP请求将不会通过控制器
我用一个简单的控制器类创建了一个简单的Maven项目,该类只显示服务器时间,但它从不经过控制器,我很高兴能得到任何帮助 我的控制器类: web.xmlJava HTTP请求将不会通过控制器,java,spring,spring-mvc,controller,Java,Spring,Spring Mvc,Controller,我用一个简单的控制器类创建了一个简单的Maven项目,该类只显示服务器时间,但它从不经过控制器,我很高兴能得到任何帮助 我的控制器类: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-a
请提供帮助?问题似乎与Tomcat有关-您是否可以查看Tomcat.home\conf文件夹中是否有web.xml文件,其中包含以下内容:
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
....
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
jsp
org.apache.jasper.servlet.JspServlet
....
jsp
*.jsp
*.jspx
这是一个解释jsp的程序
为了调试,我还要尝试的另一件事是将这些条目显式地放在您的应用程序web.xml文件中
更新:@user1067665在根据您的评论尝试了更多之后,这听起来肯定不像Tomcat的问题,更像是应用程序配置的问题。我想解决问题的办法是用
替换您对AnnotationHandlerAdapter和DefaultAnnotationHandlerMapping的定义,请您尝试一下,看看是否可行您还双重映射了您的请求。。。路径将为/index/index
从方法级注释中删除“value”参数,您可以保留“method”参数。我不明白为什么应该这样做。我会用
getServerTime()
方法创建一些实用程序类,在JSP中导入它,并将其作为时间。你能发布你得到的错误/stacktrace吗?没有任何错误输出,它显示我上面提到的索引页,但没有显示时间,甚至没有显示System.out.println(“hello”),无法打印。它从不抛出控制器类。您在浏览器中尝试了什么请求URL?URL应该是/index/indexwithout-without-value参数,我将得到相同的输出。所以这与value参数无关。我尝试了所有方法,但没有任何结果。是的,我已经检查过了,这些行位于config文件夹的web.xml中。我相信问题是Tomcat,但不知道在哪里和什么地方。我也尝试过web.xml中的这些行,但得到了相同的结果。好的,我可以请你再尝试一件事吗?你可以使用maven中的tomcat插件来启动你的应用程序,看看jsp是否仍然没有得到解释吗?这就是你如何添加一个tomcat插件:org.codehaus.mojotomcat-maven-plugin1.0,并使用mvn tomcat:runYes tomcat启动并运行。酷,当您通过maven以这种方式启动tomcat并访问您的页面时,您是否仍然会返回未解释的jsp或正确的页面?这只是为了再次发现这是应用程序问题还是tomcat问题,但仍然存在相同的问题,并且控制器没有提出任何反对意见。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- not strictly necessary for this example, but still useful, see
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference
/html/mvc.html#mvc-ann-controller for more information -->
<context:component-scan base-package="test" />
<mvc:default-servlet-handler/>
<!-- the mvc resources tag does the magic -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- also add the following beans to get rid of some exceptions -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<!-- JSTL resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Spring RESTful Example</title>
</head>
<body>
<p>Your WebApplication is up and running....</p>
<div>
Server Time is:
<div>
${serverTime}
</div>
</div>
</body>
</html>
Your WebApplication is up and running....
Server Time is:
${serverTime}
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
....
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>