我已经在Vaadin Book中解释了将Vaadin 7嵌入到基于Spring Boot的应用程序的一些JSP页面中的UI。
尽管在我调用嵌入它的路径时(通过弹簧MVC控制器)正确显示名为“v-my-vaadin-ui”的UI,但在与UI交互时出现HTTP 405错误。
网址出错:
似乎因为在Spring Boot中,所有控制器默认只允许使用GET方法(必须明确允许POST,如here所述)
我已尝试在我的Spring Security配置中禁用CSRF并配置Vaadin ralated路径:
http.authorizeRequests()
.antMatchers("/vaadinServlet/**",
"/UIDL/**",
"/v-my-vaadin-ui/UIDL/**",
"/v-my-vaadin-ui/PUSH/**",
"/HEARTBEAT/**", "/VAADIN/**").permitAll();
http.csrf().disable();
因为VAADIN集成不需要Spring管理的CSRF,但这并不能解决问题。
VAADIN应用程序非常基础:
@SpringUI(path = "v-my-vaadin-ui")
public class MyVaadinUI extends UI {
private static final long serialVersionUID = -8129815147461786549L;
@Override
protected void init(VaadinRequest vaadinRequest) {
final TextField name = new TextField("Name");
final Button greetButton = new Button("Greet");
greetButton.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
Notification.show("Hi " + name.getValue(),
Notification.Type.HUMANIZED_MESSAGE);
}
});
VerticalLayout vl = new VerticalLayout(name, greetButton);
vl.setSpacing(true);
setContent(vl);
}
}
但点击Vaadin按钮,响应代码为405,详情为:
.wsmaResponseStatusExceptionResolver:解析来自处理程序的异常[ResourceHttpRequestHandler [locations = [ServletContext资源[/],类路径资源[META-INF / resources /],类路径资源[resources /],类路径资源[静态/] ,类路径资源[public /]],resolvers = [org.springframework.web.servlet.resource.PathResourceResolver@4a8756c3]]]:org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法'POST'
不幸的是,我没有找到为UI配置POST方法的任何方法和简单的控制器,即通过
@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
因为此注释不能用于Vaadin UI。
此外,如果我使用URL直接直接 UI(即不通过Spring控制器):
用户界面显示并完美运行。
知道造成这个问题的原因是什么吗?我怎样才能允许POST方法?
答案 0 :(得分:0)
答案 1 :(得分:0)
解决!!
问题是由我在用于嵌入VAADIN应用程序的JS脚本中所犯的错误引起的(我已按照此处的文档:https://vaadin.com/docs/-/part/framework/advanced/advanced-embedding.html),特别是&#34的代码片段;的serviceUrl"参数:
"serviceUrl": "helloworld/",
我已经理解为设置了应用程序的名称,在我的情况下" v-my-vaadin-ui"。
但正确的" serviceUrl"在我的情况下,仍然是默认一个" vaadinServlet /"所以改变自:
"serviceUrl": "v-my-vaadin-ui/",
到
"serviceUrl": "vaadinServlet/",
一切正常。
下面是嵌入适用于我的vaadin应用程序的JSP标记的完整代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@tag description="ajax widget tag" pageEncoding="UTF-8"%>
<%@attribute name="v_app_url"%>
<%-- Javascript as described here: https://vaadin.com/docs/-/part/framework/advanced/advanced-embedding.html --%>
<div>
<div id="${v_app_url}" class="v-app myvaadinui" >
<div class="v-app-loading"></div>
<noscript>
You have to enable javascript in your browser to use an application built with Vaadin.
</noscript>
</div>
<script type="text/javascript">//<![CDATA[
if (!window.vaadin)
alert("Failed to load the bootstrap JavaScript: vaadinBootstrap.js");
/* The UI Configuration */
vaadin.initApplication("${v_app_url}", {
"browserDetailsUrl": "${v_app_url}/",
"serviceUrl": "vaadinServlet/",
"widgetset": "com.vaadin.DefaultWidgetSet",
"theme": "valo",
"versionInfo": {"vaadinVersion": "7.4.4"},
"vaadinDir": "/VAADIN/",
"heartbeatInterval": 300,
"debug": false,
"standalone": false,
"authErrMsg": {
"message": "Take note of any unsaved data, "+
"and <u>click here<\/u> to continue.",
"caption": "Authentication problem"
},
"comErrMsg": {
"message": "Take note of any unsaved data, "+
"and <u>click here<\/u> to continue.",
"caption": "Communication problem"
},
"sessExpMsg": {
"message": "Take note of any unsaved data, "+
"and <u>click here<\/u> to continue.",
"caption": "Session Expired"
}
});//]] >
</script>
</div>