Servlet映射URL未在Spring MVC中调用

时间:2013-06-06 10:35:32

标签: java spring servlets spring-mvc

我有一个带有web.xml配置的spring项目

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

这是我的servlet-context.xml

resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**" />

<default-servlet-handler/> 

<context:component-scan base-package="pk.training.basitMahmood.web.controller" />

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jspx" />
</beans:bean>

这是我的控制器

@RequestMapping("/contacts")
@Controller
public class ContactController {

    final Logger logger = LoggerFactory.getLogger(ContactController.class);

    @Autowired
    private ContactService contactService;

    @RequestMapping(method = RequestMethod.GET)
    public String list(Model uiModel) {

        logger.info("Listing contacts");
        List<Contact> contacts = contactService.findAll();
        uiModel.addAttribute("contacts", contacts);
        logger.info("No. of contacts: " + contacts.size());

        return "contacts/list";

    }

} //end of class ContactController

现在当我选择在服务器上运行时,我得到以下页面

enter image description here

但是当我将网址更改为http://localhost:9090/ch17_i18nSupport/contacts时,我会收到错误

enter image description here

我的联系人文件夹中有list.jspx。为什么我找不到错误?

由于

1 个答案:

答案 0 :(得分:1)

尽管Basit在评论中解决了这个问题,但我还是将其作为答案添加:

处理程序的适配器没有指示控制器中的@RequestMapping方法未被拾取。您的<mvc:annotation-driven />中有servlet-context.xml个标记吗?