集成 Spring 2.5 和遗留 Servlet 应用程序
我是春天的新手。我完成了教程中涉及 Web 流程的部分。我的最终目标是使用 Spring 在遗留 servlet webapp 中实现新功能,然后逐步替换现有代码,使 servlet webapp 成为 Spring webabb。
因此,为此,我决定再次浏览本教程的 Web 流程部分,更改名称以在遗留 servlet 应用程序的开发副本中使用 Spring 制作我自己的第一个“hello world”屏幕。
我的问题是,当我将 Spring 的 servlet 映射放入 web.xml 中时,我在尝试访问登陆页面时收到 404 错误。
- 我正在使用 WebLogic 9.2
- 我转换了我的 webapp 的目录树以模仿 Spring webapps 使用的布局
- 我将 spring.jar 和 spring-mvc.jar 复制到我的 WEB-INF/lib 中
我制作了 web.xml 的简化版本,其中只有一个遗留 servlet(用于登录页面)和 Spring。它适用于注释掉的 Spring 内容,但不适用于其他情况。 这是我的“abc”web应用程序的 WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>LogIn</servlet-name>
<servlet-class>
com.utilities.LogIn
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogIn</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<!-- Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这是我的 WEB-INF/abc-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- the application context definition for the NSD webapp DispatcherServlet -->
<beans name = "/hello.htm" class = "com.somecompany.web.HelloController"/>
</beans>
这是我的基本控制器的代码:
package com.somecompany.web;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.apache.log4j.Logger;
public class HelloController implements Controller {
protected static final Logger logger = Logger.getLogger(HelloController.class);
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.info("Returning view for CBS Search ....");
return new ModelAndView("hello.jsp");
}
}// end class
再说一次,我的问题不在于控制器或视图 (jsp)...还没有。现在,当我在 web.xml 中包含 Spring servlet 映射时,我无法访问登录页面,收到 404 错误。当我拉出 Spring servlet 映射时,这个问题就消失了。
我是 Spring 的新手,所以我不知道该去哪里寻找。
I'm new to Spring. I worked through the sections of this tutorial that cover web flow. My ultimate goal is to use Spring to implement new features in a legacy servlet webapp and then gradually replace the existing code making the servlet webapp into a Spring webabb.
So, to that end, I decided to go through the web flow part of the tutorial again, changing names to make my own first "hello world" screen with Spring within the development copy of the legacy servlet application.
My problem is that when I put the servlet mappings for Spring into my web.xml, I get 404s trying to get to my landing page.
- I'm using WebLogic 9.2
- I converted the directory tree of my webapp to mimic the layout that Spring webapps use
- I copied spring.jar and spring-mvc.jar into my WEB-INF/lib
I made a simplified version of my web.xml, with just one legacy servlet in it ( for the landing page ) and Spring. It works with the Spring stuff commented out, but not otherwise. Here it is, my WEB-INF/web.xml for the "abc" webapp
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>LogIn</servlet-name>
<servlet-class>
com.utilities.LogIn
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogIn</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<!-- Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
This is my WEB-INF/abc-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- the application context definition for the NSD webapp DispatcherServlet -->
<beans name = "/hello.htm" class = "com.somecompany.web.HelloController"/>
</beans>
Here is the code for my elementary controller:
package com.somecompany.web;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.apache.log4j.Logger;
public class HelloController implements Controller {
protected static final Logger logger = Logger.getLogger(HelloController.class);
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.info("Returning view for CBS Search ....");
return new ModelAndView("hello.jsp");
}
}// end class
Again, my problem isn't with the controller or the view (jsp)...yet. Right now, when I include the Spring servlet mappings in my web.xml, I can't get to my landing page, I get a 404. When I yank the Spring servlet mappings, that problem goes away.
I'm a raw beginner with Spring, so I am not sure where to look.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题可能出在
web.xml
中的
上。您不能使用“.jsp
”作为映射的扩展名。这个线程...但总而言之,“
*.jsp
”文件扩展名是特殊的底层 Java servlet 规范。你试图让 Spring 劫持它。这就是您链接到的教程使用“*.htm
”的原因。尝试将
web.xml
中的
更改为其他内容(例如*.htm
),看看是否可以使用该扩展名提取您的测试 URL。The issue is probably with the
<url-pattern>
in yourweb.xml
. You cannot use ".jsp
" as your mapped extension.There is some pretty detailed discussion about the low-level reasons in this thread... but the long and short of it is that the "
*.jsp
" file extension is special to the underlying Java servlet specification. You're trying to make Spring hijack that. This is why the tutorial you linked to is using "*.htm
" instead.Trying changing your
<url-pattern>
inweb.xml
to something else (such as*.htm
), and see if you can pull up your test URL with that extension.您的目标网页是什么?
/hello.htm
?您刚刚将带有htm
扩展名的所有内容映射到调度程序,但您只有一个处理hello.htm
的控制器。如果您尝试访问/index.htm
,您应该得到404
。更改您的映射或为其他 url 创建控制器。然而,如果我是你,我会在 Spring 3.0 中使用 Spring 注解来配置 Web 应用程序。这要简单得多。
What is your landing page?
/hello.htm
? your have just mapped everything withhtm
extension to a dispatcher, but you only have a controller which deals withhello.htm
. You should get404
if you are trying to access/index.htm
. Change your mapping or create a controller for other urls.However, if I were you, I would use Spring annotations in Spring 3.0 to configure the web application. It is much simpler.
正如其他人提到的,最好使用 Spring 3.0,它更容易配置。既然你已经开始了,我就谈谈我的一些想法。您需要使用诸如 SimpleUrlHandlerMapping 之类的东西,它负责将您的 url 模式解析为适当的控制器。看看这个 spring 参考文档
http://static.springsource .org/spring/docs/2.0.x/reference/mvc.html。
希望这有帮助。
As mentioned by others its better to use Spring 3.0 which is more easier to configure. Since you have started with this i am giving some of my thoughts. You need to use something like SimpleUrlHandlerMapping which takes care of resolving your url pattern to appropriate controllers. Take a look at this spring reference documentation
http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html.
Hope this helps.
尝试在 ModelAndView 视图名称上使用正斜杠:
Try a forward slash on your ModelAndView view name: