HTTP状态400:请求的资源不可用

时间:2016-01-25 15:26:01

标签: javascript java jquery jsp spring-boot

请你帮我解决这个问题,我遇到 HTTP状态400:请求的资源不可用每次我点击我的按钮编辑和删除我现在几周都无法解决这个问题有人可以就此向我提出建议吗?以下是我的代码:

用户控制器

@Controller
@RequestMapping(value = "/ims-user")
public class IMSUserController {

	private static final Logger logger = LoggerFactory.getLogger(IMSUserController.class);
	private static final String SAVE_ACTION = "/ims-user/save";
	private static final String UPDATE_ACTION = "/ims-user/update";

	@Autowired
	private IMSUserService imsUserService;

	@RequestMapping(method = RequestMethod.GET)
	public String loadIMSUserList(Model model) {
		logger.info("Getting all IMS Users");
		model.addAttribute("imsUserList", imsUserService.getAllIMSUsers());
		return "imsUserList";
	}

	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public String loadIMSUserForm(Model model) {
		logger.info("Creating new IMS User");
		IMSUser imsUser = new IMSUser();
		model.addAttribute("imsUser", imsUser);
		model.addAttribute("action", SAVE_ACTION);
		buildModel(model, imsUser, SAVE_ACTION);
		return "imsNewUserForm";
	}

	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public String loadIMSUserForm(Model model, @PathVariable Long id) {
		logger.info("Viewing User");
		IMSUser imsUser = imsUserService.getIMSUserById(id);
		model.addAttribute("imsUser", imsUser);
		return "imsViewUserForm";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String saveIMSUser(@Validated @ModelAttribute("imsUser") IMSUser imsUser, BindingResult result, Model model,
			RedirectAttributes redirectAttributes) {
		imsUserService.addIMSUser(imsUser);
		return "redirect:/ims-user";
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String deleteIMSUser(@PathVariable Long id) {		
		logger.info("Deleting User");		
		IMSUser imsUser = imsUserService.getIMSUserById(id);		
		imsUserService.deleteIMSUser(imsUser);
		return "redirect:/ims-user";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String editIMSUser(@PathVariable Long id, Model model, RedirectAttributes redirectAttributes) {
		
	logger.info("Loading edit for of IMSUser with the following details");
		IMSUser imsUser = imsUserService.getIMSUserById(id);
		

		if (imsUser == null) {
			return "redirect:/ims-user";
		}
		buildModel(model, imsUser, UPDATE_ACTION);

		return "imsEditUser";

查看-USER.JSP

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<html>

<head>

<link rel="stylesheet" type="text/css"
	href="<c:url value="/resources/css/style.css" /> ">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/resources/css/utility.css" /> ">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/resources/css/bootstrap/bootstrap.min.css" /> ">

</head>

<form:form modelAttribute="imsUser" action="${pageContext.servletContext.contextPath}/delete" method="GET" >
	<form:hidden path="id"/>
	<input type="button" id="btn-user-edit" value="Edit" class="btnHeader button" onclick="location.href='${pageContext.request.contextPath}/edit/{imsUser.id}'"/>
</form:form>


<body>

		<div class="form-user">
			<ul>
				<c:out value="${action}"/>
				
				<li><label><strong> User Type     :</strong></label><span>
				<c:out value="Employee"/></span></li>	
				
				<li><label><strong> Username     :</strong></label><span>
				<c:out value= "${imsUser.username}" /></span></li>
				
				<li><label><strong> Password     :</strong></label><span>
				<c:out value= "${imsUser.password}" /></span></li>
				
				<li><label><strong> Client Name     :</strong></label><span>
				<c:out value="Biolinks"/></span></li>
				
				<li><label><strong> First Name     :</strong></label><span>
				<c:out value="${imsUser.firstname}" /></span></li>
				
				<li><label><strong> Middle Name     :</strong></label><span>
				<c:out value= "${imsUser.middlename}" /></span></li>
				
				<li><label><strong> Last Name     :</strong></label><span>
				<c:out value= "${imsUser.lastname}" /></span></li>	
				  
				
				<li><label><strong> Contact Number     :</strong></label><span>
				<c:out value= "${imsUser.contactNumber}" /></span></li>
				
				<li><label><strong> E-mail Address     :</strong></label><span>
				<c:out value= "${imsUser.emailAddress}" /></span></li>
				
				<li><label><strong> Address     : </strong></label><span>
				<c:out value= "${imsUser.address}"/></span></li>		
							
				<li><label><strong> City     :</strong></label><span>
				<c:out value= "${imsUser.city}"/></span></li>
				
				<li><label><strong> Postal Code     :</strong></label><span>
				<c:out value= "${imsUser.postalCode}" /></span></li>
				
			</ul>
		
	
		</div>

</body>
</html>

<div id="deleteUserDialog" title="Please Confirm" class="hidden">
		Are you sure you want to delete this user?
</div>

<script type="text/javascript">	

function deleteUser()
{

		document.getElementById("imsUser").submit();
		
}	



</script>

修改-USER.JSP

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>


<form:form id="imsUser" modelAttribute="imsUser" action="${pageContext.servletContext.contextPath}${action}" method="GET" cssClass="form-user">
		<form:hidden path="id"/>
		<c:out value="${imsUser.id}"/>
		
		<ul>
					
				<li><label><strong> Username </strong></label>:<form:input
						type="text" path="username" /></li>
				<li><label><strong> Password </strong></label>:<form:input
						type="text" path="password" /></li>
				<li><label><strong> First Name </strong></label>:<form:input
						type="text" path="firstname" /></li>
				<li><label><strong> Middle Name </strong></label>:<form:input
						type="text" path="middlename" /></li>
				<li><label><strong> Last Name </strong></label>:<form:input
						type="text" path="lastname" /></li>
				<li><label><strong> Contact Number </strong></label>:<form:input
					type="text" path="contactNumber"/></li>			
				<li><label><strong> E-mail Address </strong></label>:<form:input
						type="text" path="emailAddress" /></li>
				<li><label><strong> Address </strong></label>:<form:input
						type="text" path="address" /></li>
				<li><label><strong> City </strong></label>:<form:input
						type="text" path="city" /></li>
				<li><label><strong> Postal Code </strong></label>:<form:input
						type="text" path="postalCode" /></li>
		
			
		</ul>
</form:form>

感谢你们的帮助!

3 个答案:

答案 0 :(得分:0)

我认为您的表单正在执行POST请求而不是DELETE / PUT。您可以查看浏览器的开发控制台以确认它。

您可以从表单传递action = delete / put作为请求参数。并将HiddenHttpMethodFilter添加到spring应用程序中。

答案 1 :(得分:0)

控制器中的类级别还有一个映射是@RequestMapping(value = "/ims-user")

该按钮应提交:

onclick="location.href='${pageContext.request.contextPath}/ims-user/edit/{imsUser.id}'"

而不是

onclick="location.href='${pageContext.request.contextPath}/edit/{imsUser.id}'"

答案 2 :(得分:0)

我看看你的代码,我会先尝试改变一些事情。

你的视图页面有身体部分的形式,我认为把它放到身体部分可以帮助一点。

按钮删除,我认为您可以更改链接(&lt; a href ...)到您的路线,并且这样,您不需要表格等。在这种情况下,您可以格式化链接到它看起来像一个按钮,例如,使用bootstrap,class =&#34; btn btn-default&#34 ;, getboostrap.com中的更多示例。

我刚才写了一个示例项目。在这个项目中,您将看到一些示例,我希望这对您有所帮助。

https://bitbucket.org/macielbombonato/apolo

抱歉我的英语不好,如果你需要帮助,请随时与我联系。

编辑添加一些代码......

在视图中查看jsp

</head>
<body>

<a href="${pageContext.request.contextPath}/ims-user/edit/{imsUser.id}" class="btn btn-default">
Edit
</a>

...

您的“删除”按钮可以使用相同的样式,但您需要将控制器更改为此工作。通常我在我的控制器中使用这样的链接:

/ims-user/entity/1

并使用删除方法

在该示例中查看此类: UserAPIController

@RequestMapping(value = "{id}", method = RequestMethod.DELETE)

这样你的网址就是

/ism-user/1

但在你的情况下,你可以在方法调用中简单地包含de id

@RequestMapping(value = "/ims-user/delete/{id}", method = RequestMethod.GET)
public String deleteIMSUser(@PathVariable Long id)

有了这个,您的删除按钮将是这样的:

<a href="${pageContext.request.contextPath}/ims-user/delete/{imsUser.id}" class="btn btn-default">
Delete
</a>

别忘了,你的标签需要保持在正确的位置,因此,你需要在身体内放置表格,按钮等。

我希望我能帮助你。