Bla*_*aze 6 java spring jsp http spring-mvc
我正在尝试向服务器发送一个http帖子,我从控制器中收到格式错误的网址异常
控制器代码
public static final String REST_SERVICE_URI = "localhost:8081/create";
Run Code Online (Sandbox Code Playgroud)
控制器中接收来自服务器的请求的方法
@RequestMapping(value="AddService",method = RequestMethod.POST)
@ResponseBody
public void addService(@ModelAttribute("servDetForm")) throws IOException{
//return dataServices.addService(tb);
URL serv;
URLConnection yc;
try {
serv = new URL(REST_SERVICE_URI);
yc = serv.openConnection();
try {
yc = serv.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader in;
in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的jsp视图
<form:form method="POST" commandName="servDetForm" action="AddService">
<table style="appearance:dialog ">
<tr>
<td>Number</td>
<td><form:input path="Numbers"/></td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
网址应为:
"http://localhost:8081/ItaxServ/create"
Run Code Online (Sandbox Code Playgroud)
或者可能
"https://localhost:8081/ItaxServ/create"
Run Code Online (Sandbox Code Playgroud)
(更新 - 我假设您的URL中有正确的路径.如果没有,那么您也需要修复它.)
"http"或"https"是解析器正在寻找的URL的协议部分.没有协议的URL不是有效的URL.(它是相对URI,只能针对另一个URL进行解析.)
(URI解析器将第一个冒号之前的内容解释为协议.在您的URL中,这意味着主机名("localhost")被错误地视为协议字符串.但是,没有注册的协议处理程序具有该名称的协议...所以解析器说的是"未知协议".)
提示:你能看出它之间的区别:
@RequestMapping(value = "AddService", method = RequestMethod.POST)
Run Code Online (Sandbox Code Playgroud)
还有这个:
@RequestMapping(value = "/create", method = RequestMethod.POST)
Run Code Online (Sandbox Code Playgroud)
"/"字符有什么意义?