英文:
Get URL parameter using Spring
问题
我有一个Spring应用程序,我想在URL中设置参数并转发到URL。例如,我在index.html中点击“显示详情”。然后跳转到/employees/show/1111。ShowController.java获取到了1111。现在我点击“显示详情”,结果是白色页面错误。我设置了ShowController.java的断点,但断点无法工作。我应该在哪里修复它?
控制器
@Controller
@RequestMapping("/employees/show/{employee.empId}/")
public class ShowController {
	@Autowired
	EmployeeService empService;
	@GetMapping
	public String details(@RequestParam("empId") String empId, Model model) {
		Employee employee = empService.getEmployeeInfo(Long.parseLong(empId)); // 在这一行设置断点
		model.addAttribute("employee", employee);
		return "view/show";
	}
}
index.html
<body>
	<table>
		<tr>
			<th>Id</th>
			<th>Name</th>
			<th></th>
		</tr>
		<tr th:each="employee : ${employees}">
			<td th:text="${employee.empId}"></td>
			<td th:text="${employee.empName}"></td>
			<td><a th:href="@{'/employees/show/' + ${employee.empId}}">显示详情</a></td>
		</tr>
	</table>
	<br>
</body>
show.html
<body>
	<div th:text="${employee.empId}"></div>
	<div th:text="${employee.empName}"></div>
</body>
英文:
I have Spring application,I want to set parameter in URL and forward to URL.
For example,I click "Show detail" in index.html.Then go to /employees/show/1111.
ShowController.java get 1111.Now I click "show detail",result is white page error.
And I set break point ShowController.java,break point cannot was not working.
Where I should fix it?
Controller
@Controller
@RequestMapping("/employees/show/{employee.empId}/")
public class ShowController {
	@Autowired
	EmployeeService empService;
	@GetMapping
	public String details(@RequestParam("empId") String empId, Model model) {
		Employee employee = empService.getEmployeeInfo(Long.parseLong(empId)); // break point at this row
		model.addAttribute("employee", employee);
		return "view/show";
	}
index.html
<body>
	<table>
		<tr>
			<th>Id</th>
			<th>Name</th>
			<th></th>
		</tr>
		<tr th:each="employee : ${employees}">
			<td th:text="${employee.empId}"></td>
			<td th:text="${employee.empName}"></td>
			<td><a th:href="@{'/employees/show/' + ${employee.empId}}">Show detail</a></td>
		</tr>
	</table>
	<br>
</body>
show.html
<body>
			<div th:text="${employee.empId}"></div>
			<div th:text="${employee.empName}"></div>
</body>
答案1
得分: 0
问题出在 URL 上。你正在使用 /employees/show/{employee.empId}/ 作为基本 URL。而 @GetMapping 没有映射到任何 URL,所以它会从 @RequestMapping("/employees/show/{employee.empId}/") 中获取完整的 URL。
@RequestParam 用于提取请求中的查询参数、表单参数,甚至文件;而 @PathVariable 则用于告诉 Spring,URI 路径的某一部分是要传递给方法的值。
所以,在你的情况下似乎出现了混淆,你使用了 @RequestParam 而不是 @PathVariable。
@Controller
@RequestMapping("/employees/show/{employee:.*}/") // 因为 Spring 会跳过点(.)后面的任何内容
....
@GetMapping
public String details(@PathVariable("empId") String empId, Model model) {....}
英文:
The problem is the url. You are using /employees/show/{employee.empId}/ as a base url. And @GetMappingis not mapped to any url, so it takes the exact url from @RequestMapping("/employees/show/{employee.empId}/").
@RequestParam is to extract query parameters, form parameters, and even files from the request, while @PathVariable is used to tell Spring that part of the URI path is a value you want passed to your method.
So, in your case it seems mixed, you are using @RequestParam instead of @PathVariable.
@Controller
@RequestMapping("/employees/show/{employee:.*}/") //since spring will skip anything after a dot(.)
....
@GetMapping
public String details(@PathVariable("empId") String empId, Model model) {....}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。





评论