将war部署到tomcat时的Spring MVC Servlet

时间:2017-06-22 20:32:18

标签: spring spring-mvc tomcat servlets web-deployment

我已经将war spring MVC部署到tomcat9。它已经在运行但是在春天我创建了getmapping(" / tesget")但是当我创建servlet时,结果getmapping是错误的。

控制器:

@GetMapping("tesget")
@ResponseStatus(HttpStatus.OK)
public List getTes2() throws Exception {
    return userService.getTes2();
}   

的servlet:

public class Servlet extends HttpServlet {

      public void init() throws ServletException
      {
          // Do required initialization
      } 

  public void doGet(HttpServletRequest request, 
          HttpServletResponse response) 
          throws ServletException, IOException {

          PrintWriter out = response.getWriter();
          out.println("<HTML>");
          out.println("<HEAD>");
          out.println("<TITLE>Servlet Testing</TITLE>");
          out.println("</HEAD>");
          out.println("<BODY>");
          out.println("Welcome to the Servlet Testing Center");
          out.println("</BODY>");
          out.println("</HTML>");
         }   

      public void destroy()
      {
          // do nothing.
      }   

}

的web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <servlet>
        <servlet-name>Servlet</servlet-name>
        <servlet-class>com.project.maven.config.Servlet</servlet-class>
        <load-on-startup>1</load-on-startup> 
    </servlet>

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

</web-app>

在我创建servlet和war之前,我经常运行,当我转到localhost:8080 / SpringNew / tesget时,结果为true,从数据库列出JSON,但是当我创建servlet时,url localhost:8080 / SpringNew / tesget错了。结果是:

欢迎来到Servlet测试中心

如何解决这个问题?感谢。

巴比

2 个答案:

答案 0 :(得分:1)

AppInitializer.java

public class AppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        ctx.setServletContext(container);

        ServletRegistration.Dynamic servlet = container.addServlet(
                "dispatcher", new DispatcherServlet(ctx));

        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }
}

HibernateConfiguration.java

@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.project.maven." })
@PropertySource(value = { "classpath:application.properties" })

public class HibernateConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.project.maven.entity" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        return dataSource;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
        return properties;        
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
       HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(s);
       return txManager;
    }

}

AppConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.project.maven")

public class AppConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
        registry.addResourceHandler("**/**")
        .addResourceLocations("classpath:/META-INF/resources/"); // harus ada folder resources di webapp/WEB-INF/
    }       

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }

答案 1 :(得分:0)

  1. 你的其他配置怎么样!
  2. 发送您的控制器类代码
  3. 如果您确定配置,请尝试使用{{1}}