使用Spring获取URL参数

huangapple go评论79阅读模式
英文:

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>

文件夹结构。
使用Spring获取URL参数

使用Spring获取URL参数

使用Spring获取URL参数

英文:

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(&quot;/employees/show/{employee.empId}/&quot;)
public class ShowController {

	@Autowired
	EmployeeService empService;

	@GetMapping
	public String details(@RequestParam(&quot;empId&quot;) String empId, Model model) {
		Employee employee = empService.getEmployeeInfo(Long.parseLong(empId)); // break point at this row
		model.addAttribute(&quot;employee&quot;, employee);
		return &quot;view/show&quot;;
	}

index.html

&lt;body&gt;
	&lt;table&gt;
		&lt;tr&gt;
			&lt;th&gt;Id&lt;/th&gt;
			&lt;th&gt;Name&lt;/th&gt;
			&lt;th&gt;&lt;/th&gt;
		&lt;/tr&gt;
		&lt;tr th:each=&quot;employee : ${employees}&quot;&gt;
			&lt;td th:text=&quot;${employee.empId}&quot;&gt;&lt;/td&gt;
			&lt;td th:text=&quot;${employee.empName}&quot;&gt;&lt;/td&gt;
			&lt;td&gt;&lt;a th:href=&quot;@{&#39;/employees/show/&#39; + ${employee.empId}}&quot;&gt;Show detail&lt;/a&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
	&lt;br&gt;
&lt;/body&gt;

show.html

&lt;body&gt;
			&lt;div th:text=&quot;${employee.empId}&quot;&gt;&lt;/div&gt;
			&lt;div th:text=&quot;${employee.empName}&quot;&gt;&lt;/div&gt;
&lt;/body&gt;

This folder structure.
使用Spring获取URL参数

使用Spring获取URL参数

使用Spring获取URL参数

答案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(&quot;/employees/show/{employee.empId}/&quot;).

@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(&quot;/employees/show/{employee:.*}/&quot;) //since spring will skip anything after a dot(.)
....

@GetMapping
public String details(@PathVariable(&quot;empId&quot;) String empId, Model model) {....}

huangapple
  • 本文由 发表于 2020年10月25日 13:27:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64520641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定