JSP中的表单动作无法调用不同的方法
我在Java中有一个代码,该代码使用弹簧框架根据JSP中的表单操作调用不同的方法。但是,它无法正常工作。将两种形式分开起作用,即当删除一个形式时。但是,它共同将相同的方法称为“表单操作”中首先声明的。
Java方法:
package com.example.demo.controller;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.example.demo.dao.AlienRepo;
import com.example.demo.model.Alien;
@Controller
public class AlienController {
@Autowired
AlienRepo repo;
@RequestMapping("/")
public String home() {
return "home.jsp";
}
@RequestMapping("add")
public String add(Alien alien)
{
repo.save(alien);
return "home.jsp";
}
@RequestMapping("get")
public ModelAndView get(String text)
{
ModelAndView mv = new ModelAndView();
int id = Integer.parseInt(text);
mv.setViewName("show.jsp");
mv.addObject("alien", repo.findById(id));
return mv;
}
}
JSP文件:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="get">
<input type="text" name="text"><br>
<input type="submit">
<form/>
<form action="add">
<input type="text" name="aid"><br>
<input type="text" name="aname"><br>
<input type="submit">
<form/>
</html>
我找不到任何错误,正如我所说的那样,它们的工作正常。因此,所有其他文件都应正确,如果需要,则应提供。您能帮我解决这个星际问题吗?
I have a code in Java that uses the Spring framework to call different methods as per form action in JSP. However, it's not working as expected. The two forms work separately i.e. when one is removed. But together it calls the same method whichever is declared first in the form action.
JAVA Method:
package com.example.demo.controller;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.example.demo.dao.AlienRepo;
import com.example.demo.model.Alien;
@Controller
public class AlienController {
@Autowired
AlienRepo repo;
@RequestMapping("/")
public String home() {
return "home.jsp";
}
@RequestMapping("add")
public String add(Alien alien)
{
repo.save(alien);
return "home.jsp";
}
@RequestMapping("get")
public ModelAndView get(String text)
{
ModelAndView mv = new ModelAndView();
int id = Integer.parseInt(text);
mv.setViewName("show.jsp");
mv.addObject("alien", repo.findById(id));
return mv;
}
}
JSP File:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="get">
<input type="text" name="text"><br>
<input type="submit">
<form/>
<form action="add">
<input type="text" name="aid"><br>
<input type="text" name="aname"><br>
<input type="submit">
<form/>
</html>
I cannot find any error and as I said independently they're working fine. So, all other files should be correct and if needed can be provided. Could you please help me in resolving this starnage issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修复了JSP文件的以下代码:
Fixed the jsp file with below code:
您必须指定您的方法参数是什么,因为您使用的是get方法,因此@requestparam注释将被使用: -
和
You have to specify what your method params are, since you are using get method so @RequestParam annotation will be used eg:-
and