比较来自世界各地的卖家的域名和 IT 服务价格

Spring MVC - DispatcherServlet 通过注释

我收到了一个申请 spring MVC.
他工作 Tomcat 7.
迄今为止,我收到了我的文件中的这一部分 web.xml:


<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/app-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>


有没有办法使用注释初始化它? 我有一堂课
MainSettings.java

, 所有豆类都是通过注释初始化的
@Bean

. 那我怎样才能 init
DispatherServlet

?
已邀请:

三叔

赞同来自:

这是一个评论的例子。 我希望它能帮到你。


public class ApplicationInitializer implements WebApplicationInitializer {

//Called first when the application starts loading.
public void onStartup/ServletContext servletContext/
throws ServletException {
System.out.println/"Inside application initializer..."/;

//Registering the class that incorporates the annotated DispatcherServlet configuration of spring
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext//;
rootContext.register/DispatcherConfig.class/;

//Adding the listener for the rootContext
servletContext.addListener/new ContextLoaderListener/rootContext//;

//Registering the dispatcher servlet mappings.
ServletRegistration.Dynamic dispatcher = servletContext.addServlet/"dispatcher", new DispatcherServlet/rootContext//;
dispatcher.setLoadOnStartup/1/;
dispatcher.addMapping/"/"/;
}

}

窦买办

赞同来自:

我正在写作,因为

导致创建未查看内容上下文内容的另一个上下文。


public class WebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {

private static final Log LOGGER = LogFactory.getLog/WebInitializer.class/;

@Override
protected Class [] getRootConfigClasses// {
/* this is where you will return you config class
* your root config class should @Import other configs
* to make them work without needing them to add there
*/
return new Class[] { ViewConfig.class };
}

@Override
protected Class [] getServletConfigClasses// {
return new Class[0];
}

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

}

要回复问题请先登录注册