java正在更改servletnameservlet中的文件名。将xml文件名转换为spring。使用contextparam和listener标记的xml。
我刚接触Spring MVC并学习它。 我正在使用SpringMVC尝试一个简单的HelloWorld程序,我的目标(任务)是更改servlet名称servlet。将xml文件名转换为spring。xml(用户定义的名称) 我已经尝试过使用init param标记,通过在param value标记中指定路径,它可以正常工作!!! 但是,我关心的是将文件名从servlet名称更改为servlet。将xml文件名转换为spring。使用上下文参数和侦听器标记的xml强>
我要参加R.E- IOException解析来自ServletContext资源的XML文档[/WEB-INF/hello servlet.XML];嵌套的异常是java。木卫一。FileNotFoundException:无法打开ServletContext资源[/WEB-INF/hello servlet.xml]
索引。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>
成功。jsp
<%@page isELIgnored="false" %>
${msg}
Hello控制器。爪哇
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;
}
}
春天。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>
网络。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。 我可以使用上下文参数和侦听器标记将预定义的文件名(servlet name servlet.xml)更改为用户定义的文件名(spring.xml)吗?我的代码有什么问题
共 (0) 个答案