我正在学习java和spring,有人可以帮助我如何在基于java的配置类(WebAppInializer)中编写下面给出的调度程序servlet配置,努力实现却无法成功。提前完成。
<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:view-controller path="/admin.htm" view-name="admin"/>
<mvc:view-controller path="/services.htm" view-name="services"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="home"/>
</beans>
我的Initializer.java
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@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[] { "/" };
}
}
答案 0 :(得分:0)
试试这个。
@Configuration
@ComponentScan("com.myproject")
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/admin.htm").setViewName("admin");
registry.addViewController("/services.htm").setViewName("services");
}
@Bean(name = "indexController")
public ParameterizableViewController indexController(){
ParameterizableViewController parameterizableViewController = new ParameterizableViewController();
parameterizableViewController.setViewName("home");
return parameterizableViewController;
}
}
@Configuration
@ComponentScan("com.myproject")
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/admin.htm").setViewName("admin");
registry.addViewController("/services.htm").setViewName("services");
}
@Bean(name = "indexController")
public ParameterizableViewController indexController(){
ParameterizableViewController parameterizableViewController = new ParameterizableViewController();
parameterizableViewController.setViewName("home");
return parameterizableViewController;
}
}