欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POST 后台404错误

程序员文章站 2024-03-20 22:53:58
...

今天写论坛的用户回复出错了,我在提交表单的时候希望后台返回一个数据
向后台POST表单信息的时候,后台显示POST url 404的错误

1.前端代码如下

<form id="form1">
        <p> <strong>来说点儿什么吧...</strong></p>
        <p><span> 您的姓名:</span>
          <input name="toName" type="text" id="toName" />`在这里插入代码片`
          *</p>
        <p><span>选择头像:</span> *</p>
        <p> <i>
      <input type="radio" value= "<%=basePath%>images/tou1.jpg" id= "1" name="toImage"  style="display:none">
      <label for="1"><img class="img-circle"  id="a" src="<%=basePath%>images/tou1.jpg"  style="width: 60px;height: 60px" "changimge(this.id)"></label></i> <i>
      <input type="radio" value= "<%=basePath%>images/tou2.jpg" id= "2" name="toImage"  style="display:none">
      <label for="2"><img class="img-circle an" id="b" src="<%=basePath%>images/tou2.jpg"  style="width: 60px;height: 60px""changimge(this.id)"></label></i> <i>
      <input type="radio" value= "<%=basePath%>images/tou3.jpg" id= "3" name="toImage"  style="display:none">
      <label for="3"><img class="img-circle" id="c" src="<%=basePath%>images/tou3.jpg"  style="width: 60px;height: 60px""changimge(this.id)"></label></i> <i>
      <input type="radio" value= "<%=basePath%>images/tou4.jpg" id= "4" name="toImage" style="display:none">
      <label for="4"><img class="img-circle" id="d" src="<%=basePath%>images/tou4.jpg"  style="width: 60px;height: 60px""changimge(this.id)"></label></i> <i>
      <input type="radio" value= "<%=basePath%>images/tou5.jpg" id= "5" name="toImage" style="display:none">
      <label for="5"><img class="img-circle" id="e" src="<%=basePath%>images/tou5.jpg"  style="width: 60px;height: 60px""changimge(this.id)"></label></i> <i>
      <input type="radio" value= "<%=basePath%>images/tou6.jpg" id= "6" name="toImage" style="display:none">
      <label for="6"><img class="img-circle" id="f" src="<%=basePath%>images/tou6.jpg"  style="width: 60px;height: 60px""changimge(this.id)"></label></i> <i>
      <input type="radio" value= "<%=basePath%>images/tou7.jpg" id= "7" name="toImage" style="display:none">
      <label for="7"><img class="img-circle" id="g" src="<%=basePath%>images/tou7.jpg"  style="width: 60px;height: 60px""changimge(this.id)"></label></i> <i></i>
    <p><span class="tnr">留言内容:</span></p>
      <textarea name="content" cols="60" rows="12" id="content"></textarea>
    </p>
    <input name="blogId" type="hidden" id="blogId" value="${blog.id }" />
    </form>
    <p>
      <input type="button"  onclick ="createtourist()" value="提交" />
    </p>

2.js代码

//新建留言
	function createtourist() {
	var toName=$("#toName");
	var content = $("#content");
		 if (toName == "") {
		      alert("请输入昵称");
		      $("#toName").focus();
		      return false;
		  }
		  if (content.val().length < 5) {
		      alert("字数太短了~");
		      $("#content").focus();
		      return false;
		  }
	$.post("<%=basePath%>reply/toreply.action",
	$("#form1").serialize(),function(data){
	        if(data =="OK"){
	            alert("留言成功!");
	            window.location.reload();
	        }else{
	            alert("留言失败!");
	            window.location.reload();
	        }
	    });
	}

3.后端代码

@Controller
public class TouristController {
	//依赖注入
		@Autowired
		private TouristService touristService;
		@RequestMapping(value="/reply/toreply.action")
		public String CreateTourist(Tourist tourist,Model model){
			Date date = new Date();
			// 得到一个timestamp格式时间,存入mysql中的时间格式"yyyy/MM/dd HH:mm:ss"
			Timestamp timestamp = new Timestamp(date.getTime());
			//给主键赋值
			String id = UUID.randomUUID().toString();
			tourist.setRetime(timestamp);
			tourist.setId(id);
			int row = touristService.createTourist(tourist);
			if(row>0)
			{
			return "OK";
			}
			else{
			return "FAIL";
			}
		}
}

这样一直报post url 404错误
后面在网上找了下知道了错误
我发现不能返回我想要的字符串,于是使用@ResponseBody来返回数据(@responsebody表示该方法的返回结果直接写入HTTP response body中一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。比如异步获取json数据,加上@responsebody后,会直接返回json数据)于是返回结果成功。
@Responsebody原理
该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。注意到使用@ResponseBody将会跳过视图处理部分,调用合适的HttpMessageConverter,将返回值写入输出流。