我是Spring-MVC的新手并且正在学习它。 我正在使用Spring-MVC尝试一个简单的hello-world程序,我的目标(任务)是将servlet-name-servlet.xml文件名更改为spring.xml(用户定义的名称) 我已经尝试使用init-param标记,通过在param-value标记中指定路径,它的工作正常! 但是,我关心的是使用context-param和listener标签将文件名从servlet-name-servlet.xml文件名更改为spring.xml。
我正在接受R.E - IOException从ServletContext资源[/WEB-INF/hello-servlet.xml]解析XML文档;嵌套异常是java.io.FileNotFoundException:无法打开ServletContext资源[/WEB-INF/hello-servlet.xml]
的index.jsp
<html>
<body>
<h2>Hello World!</h2>
<form action="./hello.htm">
<pre>
Enter Name: <input type="text" name="name">
<input type="submit" name="submit">
</pre>
</form>
</body>
</html>
的success.jsp
<%@page isELIgnored="false" %>
${msg}
HelloController.java
public class HelloController extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse resp) throws Exception {
String name = req.getParameter("name");
Map m = new HashMap();
m.put("msg","Hello..."+ name);
ModelAndView mav = new ModelAndView("success",m);
return mav;
}
}
spring.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:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Handler -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">h</prop>
</props>
</property>
</bean>
<!-- Controller -->
<bean id="h" class="com.controller.HelloController">
</bean>
<!-- ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/output/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
的web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- using init-param the code is working fine -->
<!--<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</init-param> -->
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
我已经使用maven依赖项添加了所有必需的jar。 我可以使用context-param和listener标签将预定义的文件名(servlet-name-servlet.xml)更改为用户定义的文件名(spring.xml)吗?我的代码出了什么问题?