我想在点击链接时提交表单,但是当我点击该元素时,它不会阻止默认操作。我也收到此消息错误:HTTP Status 405 - Request method 'POST' not supported
。这是我到目前为止所尝试的:
users.jsp
<%@ include file="/WEB-INF/template/taglibs.jsp"%>
<div class="container">
<%@ include file="menu.jsp" %>
<a class="btn btn-warning addbtn buttons" href="http://localhost:8080/Catering/index/addUser">Add</a>
<div class="panel panel-default">
<table class="table table-hover">
<thead>
<tr>
<td class="hidden">Id</td>
<td>First name</td>
<td>Last name</td>
<td>User name</td>
<td class="table-row-option"></td>
<td class="table-row-option"></td>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="item">
<tr>
<td class="hidden">${item.getUserid()}</td>
<td>${item.getFname()}</td>
<td>${item.getLname()}</td>
<td>${item.getUsername()}</td>
<td class="table-row-option">
<a class="btn btn-warning buttons col-width" href="http://localhost:8080/Catering/index/editUser?id=${item.getUserid()}">Edit</a>
</td>
<td class="table-row-option">
<form:form method="post" action="#" commandName="delete">
<input type="hidden" value="${item.getUserid()}" name="id"/>
<a class="btn btn-warning buttons col-width lnk-delete" href="#">Delete</a>
</form:form>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("a.lnk-delete").click(function(event){
event.stopImmediatePropagation();
var frm = $(this).closest('form');
frm.submit();
});
});
</script>
UsersController.java
package catering.web.controller;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import catering.web.data_access.UserDataAccess;
import catering.web.data_access.UserSummaryDataAccess;
import catering.web.mapper.UserMapper;
import catering.web.model.UserModel;
import catering.web.model.UserModelSummary;
import catering.web.view_model.UserViewModel;
@Controller
public class UsersController {
protected static Logger logger = Logger.getLogger("controller");
static final String guid = "1390e9e7-7792-4e24-b66f-6da90b8ebefb";
@RequestMapping(value = "users", method = RequestMethod.GET)
public String indexUsers(Model model, Authentication authentication){
logger.debug("Received request to users page(GET)");
List<UserModelSummary> users = UserSummaryDataAccess.getUsersSummary();
model.addAttribute("username", "You are logged in as " + authentication.getPrincipal());
model.addAttribute("list", users);
return "users";
}
@RequestMapping(value = "/addUser", method = RequestMethod.GET)
public String addUserPage(Model model,Authentication authentication){
logger.debug("Received request to show add user page(GET)");
populateDrop(model);
model.addAttribute("username", "You are logged in as " + authentication.getPrincipal());
model.addAttribute("addForm", new UserViewModel());
return "adduser";
}
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public String addUser(@ModelAttribute("addForm") UserViewModel model, Model viewmodel){
logger.debug("Received request to show add user page(POST)");
int isValid = 0;
if(model.getConfirmedpassword().equals(model.getPass())){
//map the view model to database model
UserModel user = UserMapper.mapUVMtoUM(model);
isValid = UserDataAccess.insertUser(user);
if(isValid != -1){
return "redirect:users";
} else {
viewmodel.addAttribute("error", "The username already exists!");
return "adduser";
}
} else{
viewmodel.addAttribute("passeorr", "Password unmatch!");
return "adduser";
}
}
@RequestMapping(value = "/editUser", method = RequestMethod.GET)
public String getEditUserPage(@RequestParam int id, Model model, Authentication authentication) {
logger.debug("Received request to show edit user page(GET)");
model.addAttribute("username", "You are logged in as " + authentication.getPrincipal());
populateDrop(model);
UserModel user = UserDataAccess.getUserById(id);
UserViewModel userVM = UserMapper.mapUMtoUVM(user);
userVM.setPass(guid);
userVM.setConfirmedpassword(guid);
model.addAttribute("id", userVM.getId());
model.addAttribute("firstName", userVM.getFirstName());
model.addAttribute("lastName", userVM.getLastName());
model.addAttribute("role", userVM.getRole());
model.addAttribute("user_name", userVM.getUser_name());
model.addAttribute("pass", userVM.getPass());
model.addAttribute("editForm", userVM);
return "edituser";
}
@RequestMapping(value = "/editUser", method = RequestMethod.POST)
public String editUser(@ModelAttribute("editForm") UserViewModel model, Model viewmodel){
logger.debug("Received request to show edit user page(POST)");
if(model.getRole().equals("NONE")){
viewmodel.addAttribute("error", "You need to select a role!");
return "edituser";
}
if(model.getPass().equals(model.getConfirmedpassword()) && model.getPass().equals(guid)){
UserModel user = UserMapper.mapUVMtoUM(model);
int inserted = UserDataAccess.updateWithoutPassword(user);
if(inserted != -1){
return "redirect:users";
} else{
viewmodel.addAttribute("error", "The username already exists!");
return "edituser";
}
} else {
UserModel user = UserMapper.mapUVMtoUM(model);
int inserted = UserDataAccess.updateWithPassword(user);
if(inserted != -1){
return "redirect:users";
} else{
viewmodel.addAttribute("error", "The username already exists!");
return "edituser";
}
}
}
@RequestMapping(value = "#", method = RequestMethod.POST)
public String deleteUser(@ModelAttribute("delete") @RequestParam int id){
logger.debug("Received request to delete user page(POST)");
UserDataAccess.deleteUser(id);
return "redirect:users";
}
private void populateDrop(Model model){
List<String> roles = new ArrayList<String>();
roles.add("ADMIN");
roles.add("USER");
model.addAttribute("roles",roles);
}
}
答案 0 :(得分:0)
在下面的代码中,
<form:form method="post" action="#" commandName="delete">
<input type="hidden" value="${item.getUserid()}" name="id"/>
<a class="btn btn-warning buttons col-width lnk-delete" href="#">Delete</a>
</form:form>
action="#"
不是映射请求的有效网址。因为它加载相同的
所以尝试使用控制器中的新名称映射它
希望这会有所帮助!!