1

请注意,我在 struts 1.x 中有现有项目,并且通过以下步骤,我正在尝试将 Spring MVC 集成到它。

我有一个 maven 项目,它在部署时能够读取 servlet 并在服务器启动时从jar 文件加载相应的 Controller 类,并提供有关控制器类的以下信息:

[org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping] 拒绝 bean 名称“SSOController”:未识别 URL 路径

看起来它没有加载 URL,因此我无法对其进行任何安静的 Web 服务调用。

我的 web.xml 有以下条目:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/hello.do</url-pattern>
</servlet-mapping>

Dispatcher Servlet 具有以下条目:

<context:annotation-config />
<context:component-scan base-package="com.ga.action.controller" />

<mvc:resources location = "/resources/" mapping = "/resources/**" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean> 



<!-- JAXB2 marshaller. Automagically turns beans into xml -->
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.ga.action.controller.PEMUtil</value>
</list>
</property>
 </bean>

控制器类代码如下:

@Controller
@RequestMapping("/hello.do")
public class TestController {

    @RequestMapping(method = RequestMethod.GET, value = "/")
    public @ResponseBody String  printTokenMessage(
            @RequestParam("nLiveToken") String message, 
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        System.out.println(message);
    }
}
4

2 回答 2

2

对外部 jar 文件控制器使用 XML 配置而不是 @Conteroller 注释。因为 @Controller 注释在服务器类加载器中不可用。 http://forum.spring.io/forum/spring-projects/web/108774-controllers-from-external-jar

于 2014-01-30T11:02:37.233 回答
0

Since the exception indicates the use of BeanNameUrlHandlerMapping there should be a bean named /hello.do in your context.

You do use the @RequestMapping annotation so I guess you assumed annotation driven handler mapping would be enabled by default. Which isn't the case. If the ServletDispatcher finds no handler mapping it will create a BeanNameUrlHandlerMapping for you.

If you want to use annotations in your application you should define

<mvc:annotation-driven />

in your context configuration.

于 2013-10-29T08:17:28.890 回答