我对Spring很新,遇到RequestMapping
的问题。我想学习使用注释,所以我尝试创建一个适合这些的小项目。无论如何,我一直得到404,但据我所知,我做的一切都是正确的。对于一些帮助会非常高兴,在阅读几个教程时无法找到解决方案,这些教程基本相同。谢谢!
WEB.XML:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
分派器-servlet.xml中:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="com.controller" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<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>
Controller&#34; MainController.java&#34;在com.controller包中:
package com.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
/**
*
* @author
*/
@Controller
public class MainController{
@RequestMapping("/main")
public ModelAndView hithere() {
ModelAndView mav = new ModelAndView ("main");
mav.addObject("welcomeMessage", "Hi there");
return mav;
}
}
然后在Web-inf / jsp文件夹中有一个main.jsp。
答案 0 :(得分:0)
将调度程序servlet的url映射更改为:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
或者将您的RequestMapping(value="/main")
更改为RequestMapping(value="/main.htm")
答案 1 :(得分:0)
哦很好找到了..
因此在web.xml中缺少这个:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
这个
$country = 'UNITED STATES OF AMERICA';
function countryToLowerCase( $country )
{
$except = array('of','in');
$parts = explode(' ', $country);
$country = null;
foreach ($parts as $part) {
if(!in_array( $part, $except))
{
$country .= ucfirst($part) .' ';
continue;
}
$country .= $part .' ';
}
return $country;
}
echo countryToLowerCase( strtolower($country));//United States of America
从调度程序移到applikation.xml。没注意这一点,因为netbeans以这种方式生成它并且我信任它:D