我有一个基于Spring 4.3.20和Hibernate 3.6构建的现有应用程序,我们在XML配置中使用protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = findViewById(R.id.your_button);
//This will give NullPointerException as you're trying to find
//button before activity gets any layout to work with
setContentView(R.layout.activity_main);
}
进行控制器映射,并且运行良好。
我从较小的步骤开始,没有进行任何更改,只是将我的maven项目仅移至spring-boot依赖项(没有SimpleUrlHandlerMapping
带注释的文件),并且一切正常。
现在,我开始将@SpringBootApplication
的内容移动到web.xml
文件中,但是它不起作用,不确定我丢失了什么。
控制器
SpringBootApplication
dispatcher-servlet.xml
public class AuthController extends MultiActionController {
public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws ServletException {
return new ModelAndView("jsonView","model",<data in string>,);
}
}
views.xml
<beans>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="AuthController/*.do">AuthController</prop>
</props>
</property>
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor" />
</list>
</property>
</bean>
</beans>
JsonView.java
<bean name="jsonView" class="com.mycompany.views.JsonView">
<property name="contentType">
<value>text/html; charset=ISO-8859-1</value>
</property>
</bean>
我们已为SpringBootApplication删除的web.xml 内容
public class JsonView extends AbstractView {
boolean validSession = true;
public void setValidSession(boolean validSession) {
this.validSession = validSession;
}
protected void renderMergedOutputModel(Map map,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String model = (String) map.get("model");
try {
JSONObject jobj = new JSONObject(model);
if (jobj.has("msg")) {
if (jobj.getString("msg").toLowerCase().contains("system failure:")) {
jobj.remove("msg");
jobj.put("msg", "Some problem occurred while performing the operation. Please try again later.");
model = jobj.toString();
}
}
} catch (JSONException je) {
model = (String) map.get("model");
}
model = "{\"valid\":" + this.validSession + ",\"data\":" + model + ",\"success\":true}";
response.getOutputStream().write(model.getBytes());
response.setContentType("text/html; charset=UTF-8");
}
SpringBootApplication
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/applicationContextList.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/dispatcher-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
applicationContext-view.xml
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class })
@ImportResource("classpath*:config/applicationContextList.xml")
public class MainSpringBootApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(MainSpringBootApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MainSpringBootApplication.class);
}
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet());
Map<String,String> params = new HashMap<String,String>();
params.put("contextClass","org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
params.put("contextConfigLocation","classpath:config/dispatcher-servlet.xml");
registration.setInitParameters(params);
registration.addUrlMappings("*.do");
registration.addUrlMappings("/props/*");
return registration;
}
applicationContextList.xml
<bean id="xmlViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="order" value="1" />
<property name = "location">
<value>/WEB-INF/views.xml</value>
</property>
</bean>
当js调用<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<import resource="applicationContext.xml" />
<import resource="applicationContext-view.xml" />
</beans>
时,它会产生404错误,而在没有SpringBootApplication和仅AuthController.test.do
的情况下可以正常工作
当我执行所需的服务时,我得到以下日志:
web.xml