SpringMVC控制器不起作用

时间:2016-12-26 08:29:32

标签: java spring spring-mvc

我正在学习SpringMVC并尝试构建一个HelloWorld webapp 我使用来自 Sping in Action 4th Edition 的代码,使用eclipse构建了这个项目 但是当我通过访问http://localhost:8080/homepage在浏览器上测试时,我收到了404错误 enter image description here

最令人厌烦的是,如果我使用MockMvc测试控制器( Spring in Action 提供的方法,它将通过测试。
所以我想知道我哪里做错了?

我的项目结构:
enter image description here

SpittrWebAppInitializer.java:

// I have written this code inside Fragment_A

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view= inflater.inflate(R.layout.fragment_all_bill_, container, false);
            tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);

            tabLayout.addTab(tabLayout.newTab().setText("Bill"));
            tabLayout.addTab(tabLayout.newTab().setText("Mybill"));    
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
            tabLayout.setSelectedTabIndicatorColor(getActivity().getResources().getColor(R.color.white));
            pager = (ViewPager) view.findViewById(R.id.pager);

            ViewPagerAdapter adapter=new ViewPagerAdapter(getActivity().getSupportFragmentManager());
            pager.setAdapter(adapter);

            pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
            {
                @Override
                public void onTabSelected(TabLayout.Tab tab)
                {
                    pager.setCurrentItem(tab.getPosition());
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });

            return view;

// This is my adapter class
public class ViewPagerAdapter extends FragmentStatePagerAdapter
{
    public ViewPagerAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int position)
    {
        switch (position)
        {
            case 0:

                return new Bills_Fragment();
            case 1:
                return new MyBill_Fragment();

                 }
        return null;
    }

    @Override
    public int getCount()
    {
        return 2;
    }

    public int getItemPosition(Object object)
    {
        Log.d("position===","case2noone");
        return POSITION_NONE;
    }

}


WebConfig.java

package spittr.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return new Class<?>[] {RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return new Class<?>[] {WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[]{"/"};
    }

}


RootConfig.java

package spittr.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("spitter.web")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewRseolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
        configurer.enable();
    }

}


HomeController.java

package spittr.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages={"spitter"},excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})
public class RootConfig {

}

1 个答案:

答案 0 :(得分:2)

@ComponentScan("spitter.web")

您的包名是spittr.web

@ComponentScan(basePackages={"spitter"}

同样在这里

添加说明:

@ComponentScan正在查找提供的包及其所有子包上的@Component(包括@ Repository,@ Service和@Controller)注释类,以便将它们添加到Spring Context中。虽然提供的包不存在,但Spring找不到您的控制器,因此它不会创建它。

当你测试它时它会起作用,因为你在测试中明确地使用它。