更改Spring MVC的根URL

时间:2016-03-25 19:08:02

标签: spring spring-mvc spring-security

当我运行我的项目时,启动网址为 http://localhost:8080/shah/

我希望将网址设为 http://localhost:8080/shah/welcome

这里,控制器的方法有@RequestMapping(" / welcome")

在哪里更改根网址?

我正在使用基于注释的配置和以下类:

Initializer.java

@Order(1)
public class Initializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {DatabaseConfig.class };
    }

     @Override
    protected Class<?>[] getServletConfigClasses() {
         return new Class<?>[] { WebAppConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

WebAppConfig.java

@Configuration
@EnableWebMvc
@ComponentScan("com.ism.shah")
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

    // Maps resources path to webapp/resources
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations(
            "/resources/");
    }

    // Provides internationalization of messages
    @Bean
    public ResourceBundleMessageSource messageSource() {
        ResourceBundleMessageSource source = new 
                         ResourceBundleMessageSource();
        source.setBasename("messages");
        return source;
    }

 }

DatabaseConfig.java

@Configuration
@EnableTransactionManagement
@ComponentScan("com.ism.shah")
@PropertySource("classpath:database.properties")
public class DatabaseConfig {

private static final String PROPERTY_NAME_DATABASE_DRIVER   = "db.driver";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
private static final String PROPERTY_NAME_DATABASE_URL      = "db.url";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";

private static final String PROPERTY_NAME_HIBERNATE_DIALECT = 
                "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = 
                "hibernate.show_sql";
private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = 
                 "entitymanager.packages.to.scan";

@Resource
private Environment env;

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setDriverClassName
    (env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
    dataSource.setUrl(env.getRequiredProperty
            (PROPERTY_NAME_DATABASE_URL));
    dataSource.setUsername(env.getRequiredProperty
                 (PROPERTY_NAME_DATABASE_USERNAME));
    dataSource.setPassword(env.getRequiredProperty
             (PROPERTY_NAME_DATABASE_PASSWORD));

    return dataSource;
}

private Properties hibProperties() {
    Properties properties = new Properties();
    properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, 
         env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
    properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, 
         env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
    return properties;  
}

@Bean
public HibernateTransactionManager transactionManager() {
    HibernateTransactionManager transactionManager = new 
        HibernateTransactionManager();
    transactionManager.setSessionFactory(sessionFactory().getObject());
    return transactionManager;
}

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactoryBean = new 
          LocalSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource());

      sessionFactoryBean.setPackagesToScan
      (env.getRequiredProperty(
        PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
    sessionFactoryBean.setHibernateProperties(hibProperties());
    return sessionFactoryBean;
}
}

1 个答案:

答案 0 :(得分:0)

如果您使用Eclipse或仅使用STS 步骤1。从服务器选项卡下的服务器中删除项目。
第2步。右键单击项目,选择“属性”,“Web项目设置”,在此处更新上下文根&#39; shah&#39; to shah / welcome&#39;。
然后,您可以在受尊重的服务器上再次运行项目。