搜尋

首頁  >  問答  >  主體

java - spring mvc 只要使用context:component-scan标签就报500错误

使用的是jre1.8 spring4.0 让我百思不得其解的是只要springmvc.xml中出现了
<context:component-scan base-package="com.ssm.controller"></context:component-scan> 这个标签 就肯定会报下面的这个500错误,只要不写这个标签就能成功部署,我后来用标注的方式来标记handler还是不行 一切都是因为这个标签的问题 已经好几天了无法解决 求高人看下这个到底是哪里的错误 我实在搞不懂,多谢了!

springmvc.xml 配置文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

<code><?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:mvc="http://www.springframework.org/schema/mvc"

       xmlns:p="http://www.springframework.org/schema/p"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xmlns:tx="http://www.springframework.org/schema/tx"

       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

            http://www.springframework.org/schema/aop

            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

            http://www.springframework.org/schema/tx

            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

            http://www.springframework.org/schema/mvc

            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

            http://www.springframework.org/schema/context

            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

     

    

 <context:component-scan base-package="com.ssm.controller"></context:component-scan>

    

   

   

  <!-- 处理器适配器 将bean的name作为url进行查找 ,需要在配置Handler时指定beanname(就是url)

    所有的映射器都实现 HandlerMapping接口。 -->

  <bean   class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

     

     

     

      <!-- 配置Handler  将编写的Handller在映射器加载 ,name一定要写成url -->

    <bean  id="itemsController" name="/queryItems.action" class="com.ssm.controller.ItemsController1" />

       

       

    <!-- 处理器映射器 将bean的name作为url进行查找 ,需要在配置Handler时指定beanname(就是url)

    所有的映射器都实现 HandlerMapping接口。

    -->

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

   

   

    <!-- 视图解析器

    解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包

     -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

       

    </bean>

</beans>

 

 

 

 

 

 

</code>

web.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

<code> <?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>springmvc</display-name>

     

  <!-- springmvc前端控制器 -->

  <servlet>

      <servlet-name>springmvc</servlet-name>

      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

         

      <init-param>

          <param-name>contextConfigLocation</param-name>

          <param-value>classpath:springmvc.xml</param-value>

      </init-param>

  </servlet>

     

  <servlet-mapping>

      <servlet-name>springmvc</servlet-name>

     

      <url-pattern>*.action</url-pattern>

  </servlet-mapping>

     

     

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

</web-app>

 

 

</code>


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

<code>

 

**ItemsController1**

 

    package com.ssm.controller;

   

import java.util.ArrayList;

import java.util.List;

   

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

   

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.Controller;

   

import com.ssm.pojo.Items;

   

public class ItemsController1 implements Controller{

   

       

    public ModelAndView handleRequest(HttpServletRequest request,

            HttpServletResponse response) throws Exception {

           

        //调用service查找 数据库,查询商品列表,这里使用静态数据模拟

                List<Items> itemsList = new ArrayList<Items>();

                //向list中填充静态数据

                   

                Items items_1 = new Items();

                items_1.setName("联想笔记本");

                items_1.setPrice(6000f);

                items_1.setDetail("ThinkPad T430 联想笔记本电脑!");

                   

                Items items_2 = new Items();

                items_2.setName("苹果手机");

                items_2.setPrice(5000f);

                items_2.setDetail("iphone6苹果手机!");

                   

                itemsList.add(items_1);

                itemsList.add(items_2);

   

                //返回ModelAndView

                ModelAndView modelAndView =  new ModelAndView();

                //相当 于request的setAttribut,在jsp页面中通过itemsList取数据

                modelAndView.addObject("itemsList", itemsList);

                   

                //指定视图

                modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");

   

                return modelAndView;

   

    }

   

}</code>

迷茫迷茫2903 天前961

全部回覆(4)我來回復

  • 怪我咯

    怪我咯2017-04-17 17:59:32

    com.ssm.controller.*試試

    回覆
    0
  • 黄舟

    黄舟2017-04-17 17:59:32

    1

    2

    3

    4

    <code><listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

      </listener>

    </code>

    web.xml少了上面這句,把Spring容器整合到 Web 應用裡面

    回覆
    0
  • 阿神

    阿神2017-04-17 17:59:32

    樓上正解,因為你沒有在web伺服器啟動的時候載入spring容器。我只是多此一舉摻和一下~~~~

    回覆
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:59:32

    樓主,我的問題跟你一模一樣,弄了很久都找不到原因,你怎麼解決的啊?

    回覆
    0
  • 取消回覆