我在视图中有这个代码来显示输入框和提交按钮。 (修订版)
<form action="/hms/accommodations" method="GET">
<input type="text" name="searchKeyword"/>
<input type="submit" value="Submit"/>
</form>
当我单击提交按钮时,我需要将用户键入的任何内容传递到输入框中,以传递给我的Controller中的方法。
这是我在Controller中的方法。 (修订版)
@RequestMapping(value = "/hms/accommodations", method = RequestMethod.GET)
public List<Accommodation> listofAccomo(@RequestParam String searchKeyword){
List<Accommodation> searchResult = accommodationService.findAllAccommodationBySearchBox(searchKeyword);
return searchResult;
}
我在我的AccommodationService类中有这个:
public abstract List<Accommodation> findAllAccommodationBySearchBox(String searchKeyword);
这是另一个课程:
public List<Accommodation> findAllAccommodationBySearchBox(String searchKeyword){
TypedQuery<Accommodation> searchResult = em.createNamedQuery("findAllAccommodationBySearch", Accommodation.class);
searchResult.setParameter("searchKeyword",'%'+searchKeyword+'%');
List<Accommodation> result=searchResult.getResultList();
return result;
}
我的域名中有这个NamedQuery。:
@NamedQueries(
{
@NamedQuery(
name = "findAllAccommodationBySearch",
query = "SELECT a FROM Accommodation a WHERE a.person.firstName LIKE :searchKeyword OR a.person.middleName LIKE :searchKeyword OR a.person.lastName LIKE :searchKeyword OR a.room.roomNumber LIKE :searchKeyword OR a.person.pvId LIKE :searchKeyword OR a.startDate LIKE :searchKeyword OR a.endDate LIKE :searchKeyword"
)
})
当我在输入后单击我的提交按钮时,它会重定向到相同的页面,但是使用不同的URL,但它似乎无法访问我的Controller方法,因为它不会返回它应该返回的值。 我在这里错过了什么?希望有人能提供帮助。谢谢!
编辑:这是我的Controller类代码。
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriUtils;
import org.springframework.web.util.WebUtils;
import ph.com.smesoft.hms.domain.Accommodation;
import ph.com.smesoft.hms.service.AccommodationService;
import ph.com.smesoft.hms.service.PersonService;
import ph.com.smesoft.hms.service.RoomService;
@Controller
@RequestMapping("/accommodations")
public class AccommodationController {
@RequestMapping(value = "/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<String> showJson(@PathVariable("id") Long id) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json; charset=utf-8");
try {
Accommodation accommodation = accommodationService.findAccommodation(id);
if (accommodation == null) {
return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
}
return new ResponseEntity<String>(accommodation.toJson(), headers, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<String>("{\"ERROR\":"+e.getMessage()+"\"}", headers, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
[1:49:35 AM] Kath Revilla: @RequestMapping(headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<String> listJson() {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json; charset=utf-8");
try {
List<Accommodation> result = accommodationService.findAllAccommodations();
return new ResponseEntity<String>(Accommodation.toJsonArray(result), headers, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<String>("{\"ERROR\":"+e.getMessage()+"\"}", headers, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json")
public ResponseEntity<String> createFromJson(@RequestBody String json, UriComponentsBuilder uriBuilder) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
try {
Accommodation accommodation = Accommodation.fromJsonToAccommodation(json);
accommodationService.saveAccommodation(accommodation);
RequestMapping a = (RequestMapping) getClass().getAnnotation(RequestMapping.class);
headers.add("Location",uriBuilder.path(a.value()[0]+"/"+accommodation.getId().toString()).build().toUriString());
return new ResponseEntity<String>(headers, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<String>("{\"ERROR\":"+e.getMessage()+"\"}", headers, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping(value = "/jsonArray", method = RequestMethod.POST, headers = "Accept=application/json")
public ResponseEntity<String> createFromJsonArray(@RequestBody String json) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
try {
for (Accommodation accommodation: Accommodation.fromJsonArrayToAccommodations(json)) {
accommodationService.saveAccommodation(accommodation);
}
return new ResponseEntity<String>(headers, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<String>("{\"ERROR\":"+e.getMessage()+"\"}", headers, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, headers = "Accept=application/json")
public ResponseEntity<String> updateFromJson(@RequestBody String json, @PathVariable("id") Long id) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
try {
Accommodation accommodation = Accommodation.fromJsonToAccommodation(json);
accommodation.setId(id);
if (accommodationService.updateAccommodation(accommodation) == null) {
return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
}
return new ResponseEntity<String>(headers, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<String>("{\"ERROR\":"+e.getMessage()+"\"}", headers, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Autowired
AccommodationService accommodationService;
@Autowired
PersonService personService;
@Autowired
RoomService roomService;
@RequestMapping(produces = "text/html")
public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, @RequestParam(value = "sortFieldName", required = false) String sortFieldName, @RequestParam(value = "sortOrder", required = false) String sortOrder, Model uiModel) {
if (page != null || size != null) {
int sizeNo = size == null ? 10 : size.intValue();
final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
uiModel.addAttribute("accommodations", Accommodation.findAccommodationEntries(firstResult, sizeNo, sortFieldName, sortOrder));
float nrOfPages = (float) accommodationService.countAllAccommodations() / sizeNo;
uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
} else {
uiModel.addAttribute("accommodations", Accommodation.findAllAccommodations(sortFieldName, sortOrder));
}
addDateTimeFormatPatterns(uiModel);
return "accommodations/list";
}
@RequestMapping(value = "/hms/accommodations", method = RequestMethod.GET)
public List<Accommodation> listofAccomo(@RequestParam String searchKeyword){
List<Accommodation> searchResult = accommodationService.findAllAccommodationBySearchBox(searchKeyword);
return searchResult;
}
void addDateTimeFormatPatterns(Model uiModel) {
uiModel.addAttribute("accommodation_startdate_date_format","yyyy-MM-dd");
uiModel.addAttribute("accommodation_enddate_date_format","yyyy-MM-dd");
}
void populateEditForm(Model uiModel, Accommodation accommodation) {
uiModel.addAttribute("accommodation", accommodation);
addDateTimeFormatPatterns(uiModel);
uiModel.addAttribute("people", personService.findAllPeople());
uiModel.addAttribute("rooms", roomService.findAllRooms());
}
String encodeUrlPathSegment(String pathSegment, HttpServletRequest httpServletRequest) {
String enc = httpServletRequest.getCharacterEncoding();
if (enc == null) {
enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
}
try {
pathSegment = UriUtils.encodePathSegment(pathSegment, enc);
} catch (UnsupportedEncodingException uee) {}
return pathSegment;
}
}
答案 0 :(得分:2)
问题似乎是在您的控制器方法中您期望路径参数,而在表单中您传递查询参数。 我会改变方法来接受像
这样的查询参数@RequestMapping(value = "/hms/accommodations", method = RequestMethod.GET)
public List<Accommodation> listofAccomo(@RequestParam String searchKeyword){
List<Accommodation> searchResult = accommodationService.findAllAccommodationBySearchBox(searchKeyword);
return searchResult;
}
有两处变化:
此外,在表单中您不需要操作中的查询参数部分,因为在提交期间它将被浏览器替换,并且数据将作为查询参数附加:
<form action="/hms/accommodations" method="GET">
<input type="text" name="searchKeyword"/>
<input type="submit" value="Submit"/>
</form>
答案 1 :(得分:0)
问题在于您当前的代码,最终的网址如下所示: 的 /住宿/ HMS /住宿强>
换句话说,在类级别的总url = @RequestMapping +在方法级别的@RequestMapping (总url是弹簧处理程序映射看到的&amp; then maps / identify来调用相应的方法)
选项(1):
因此,为了使其正常工作,您需要删除顶部(班级)的@RequestMapping(&#34; / accommodation&#34;)
控制器类:
@Controller
public class AccommodationController {
@RequestMapping(value = "/hms/accommodations", method = RequestMethod.GET)
public List<Accommodation> listofAccomo(@RequestParam String searchKeyword){
List<Accommodation> searchResult = accommodationService.findAllAccommodationBySearchBox(searchKeyword);
return searchResult;
}
//add all other methods
}
<强> HTML:强>
<form action="/hms/accommodations" method="GET">
<input type="text" name="searchKeyword"/>
<input type="submit" value="Submit"/>
</form>
选项(2): 您可以通过以下操作更改您的表单&#34; / accomances / hms / accommodation&#34;:
控制器类:
@Controller
@RequestMapping("/accommodations")
public class AccommodationController {
@RequestMapping(value = "/hms/accommodations", method = RequestMethod.GET)
public List<Accommodation> listofAccomo(@RequestParam String searchKeyword){
List<Accommodation> searchResult = accommodationService.findAllAccommodationBySearchBox(searchKeyword);
return searchResult;
}
//add all other methods
}
<强> HTML:强>
<form action="/accommodations/hms/accommodations" method="GET">
<input type="text" name="searchKeyword"/>
<input type="submit" value="Submit"/>
</form>