我使用maven编写了一个简单的SpringMVC程序,当我单击login按钮重定向时,会抛出404错误
2017年7月18日下午8:45:43 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadbean定义信息:从ServletContext资源加载xml bean定义[/WEB-INF/hello servlet.xml]7月18日,2017年7月18日下午8:45:44 org.springframework.web.servlet.DispatcherServlet initServletBean信息:FrameworkServlet“hello”:初始化在580毫秒内完成2017年7月18日下午8:45:44 org.springframework.web.servlet.PageNotFound noHandlerFound警告:在名为“hello”的DispatcherServlet中找不到URI为[/HelloWorld/faisal]的HTTP请求的映射我使用maven编写了一个简单的SpringMVC程序,当我单击login按钮重定向时,会抛出404错误,maven,spring-mvc,Maven,Spring Mvc,2017年7月18日下午8:45:43 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadbean定义信息:从ServletContext资源加载xml bean定义[/WEB-INF/hello servlet.xml]7月18日,2017年7月18日下午8:45:44 org.springframework.web.servlet.DispatcherServlet initServletBean信息:Fram
您好servlet.xml您需要在
web.xml
文件中加载ApplicationContext.xml
,您可以使用下面的示例代码在web.xml中添加ApplicationContext.xml
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
myServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
类路径:ApplicationContext.xml
1.
首先,如果您想从控制器获取任何响应,并且方法声明为VOID您不想获取任何响应,那么您的方法将在控制台中打印字符串faisl。然后,如果要获取打印“faisal”的索引页,则需要如下声明控制器:
@Controller
public class MainClass {
@RequestMapping(value="/faisal", method=RequestMethod.GET)
public ModelAndView getIndex(){
return new ModelAndView("index", "msg", "Hello Faisal");
}
}
在您的hello servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:ctx = "http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
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-2.5.xsd">
<ctx:component-scan base-package="com.faisal"></ctx:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
${msg}
是您在控制器中传递的密钥,该值为Hello faisal
,如果您打开url
/地狱世界/费萨尔
在您看来,您应该能够看到Hello Faisal
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:ctx = "http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
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-2.5.xsd">
<ctx:component-scan base-package="com.faisal"></ctx:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
关于,我刚开始学习mvc Spring,请您对此进行详细说明。请参阅此链接:了解更多关于ApplicationContext.xml的信息。谢谢Sharma,让我阅读maven的链接,它看起来很简单,让我试试这本书。。。。你可以试试这个。。。。如果你有错误,请与我分享。。。谢谢,我可以看到index.jsp。当使用表单重定向或直接传递HelloWorld/faisal时,我得到了404,在eclipse中,我可以看到错误,org.springframework.web.servlet.PageNotFound NoHandlerFound您可以解释一下吗?您告诉我您可以看到index.jsp。你想干什么?