当前位置: 首页 > 知识库问答 >
问题:

将war部署到tomcat时的Spring MVC Servlet

羊禄
2023-03-14
@GetMapping("tesget")
@ResponseStatus(HttpStatus.OK)
public List getTes2() throws Exception {
    return userService.getTes2();
}   
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.
      }   

}
<!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测试中心

如何修复这个问题?多谢了。

共有1个答案

益楷
2023-03-14

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;
    }
 类似资料:
  • 我正在使用spring boot,我想将一个rest API war文件部署到tomcat服务器中。tomcat服务器日志中没有错误,但当我调用任何endpoint时,我得到的是404“not found”,而我从tomcat服务器得到的是相同的。 java版本:8。tomcat版本:9。 也许我错过了什么,下面是我的代码示例: Pom: 2.application.java

  • 我正在尝试将带有WAR打包的Spring Boot应用程序部署到。应用程序部署成功,但是,当我尝试访问endpoint时,它导致404找不到。 WAR文件:application.WAR Tomcat文件夹包含以下内容和(Angular)具有 正如这里所讨论的,我添加了一个类,它扩展了,如下所示 在pom.xml中,我为spring-boot-starter-tomcat、tomcat-embed

  • 问题内容: 我想将Spring Boot应用程序部署到Tomcat,因为我想部署到AWS。我创建了一个WAR文件,但是即使它可见,它似乎也不能在Tomcat上运行。 详细信息: 0。这是我的应用程序: 具有以下内容: 阅读了许多页面和问题后,我在POM中添加了以下内容: http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0 我运行了“ mvn软件包

  • > 在阅读了大量页面和问答后,我在POM中添加了以下内容: http://maven.apache.org/xsd/maven-4.0.0.xsd“>4.0.0 我运行了“MVN包”,得到了WAR文件(大小250MB),我把它放进了“webapps”文件夹。

  • 我开发了一个springboot war文件来部署到服务器中,但是由于某些原因,我不得不将它部署到xampp的tomcat中,但是我得到了如下错误。我怎么才能修好这个? 信息:validateJarFile(c:\xampp\tomcat\webapps\ipf-2.0.0.rc2\WEB-INF\lib\tomcat-embed-core-8.5.28.jar)-jar未加载。参见Servlet