Java 路线映射不起作用
我是Java web应用程序的新手。我们已经创建了简单的HelloWorld程序,下面是使用SpringMVC框架在internet上的教程。但是,我找不到路径TestWebApp/helloWorld.htm始终不能工作的原因 如果我查看GlassFish 4.1服务器日志,它会显示以下内容:Java 路线映射不起作用,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我是Java web应用程序的新手。我们已经创建了简单的HelloWorld程序,下面是使用SpringMVC框架在internet上的教程。但是,我找不到路径TestWebApp/helloWorld.htm始终不能工作的原因 如果我查看GlassFish 4.1服务器日志,它会显示以下内容: Info: Mapped URL path [/helloworld] onto handler 'helloWorldController' Info: Mapped URL path [/h
Info: Mapped URL path [/helloworld] onto handler 'helloWorldController'
Info: Mapped URL path [/helloworld/*] onto handler 'helloWorldController'
HelloWorldController.java
helloWorld.jsp
web.xml
dispatcher-servlet.xml
项目结构:
我发现了很多类似的问题,但似乎没有任何答案可以解决这个问题 控制器定义的请求映射为
@RequestMapping(value = "/helloWorld", method = RequestMethod.GET)
但你访问web应用程序的方式应该是
@RequestMapping(value = "/helloWorld.htm", method = RequestMethod.GET)
你错过了比赛
<mvc:annotation-driven />
您似乎缺少注释驱动的元素,例如,在此处选中,只有有关缺少元素的部分适用于您的注释case@Master谢谢你。这就解决了问题。@MasterSlave你能不能把它作为完整的帖子,这样我就可以接受它作为答案?是吗,标记为重复也是一个选项,但另一个问题有一个额外的问题,这是肯定需要的,因为这是你正在访问的url。你的webapp的上下文路径是什么,你用来访问页面的确切url是什么。此外,还需要添加这几行。xmlns:mvc=http://www.springframework.org/schema/mvc 在xsi中,需要添加schemaLocation:http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdyeps,在答覆中加上,并作简短解释
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<context:component-scan base-package="testapp"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
@RequestMapping(value = "/helloWorld", method = RequestMethod.GET)
@RequestMapping(value = "/helloWorld.htm", method = RequestMethod.GET)
<mvc:annotation-driven />
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"