JSP中的表单动作无法调用不同的方法

发布于 2025-01-21 11:07:48 字数 1759 浏览 6 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晌融 2025-01-28 11:07:49

修复了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>
</body>
</html>

Fixed the jsp file with below code:

<%@ 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>
</body>
</html>
国产ˉ祖宗 2025-01-28 11:07:48

您必须指定您的方法参数是什么,因为您使用的是get方法,因此@requestparam注释将被使用: -

    @RequestMapping("/add")
public String add(@RequestParam("aid") String aid, @RequestParam("aname") String aname)
{
    Alien alien = new Alien();
    alien.setId(aid);
    alien.setName(aname);
    repo.save(alien);
    return "home.jsp";
}

    @RequestMapping("/get")
public ModelAndView get(@RequestParam("text") String text)
{
    ModelAndView mv = new ModelAndView();
    int id = Integer.parseInt(text);
    mv.setViewName("show.jsp");
    mv.addObject("alien", repo.findById(id));
    return mv;
}

You have to specify what your method params are, since you are using get method so @RequestParam annotation will be used eg:-

    @RequestMapping("/add")
public String add(@RequestParam("aid") String aid, @RequestParam("aname") String aname)
{
    Alien alien = new Alien();
    alien.setId(aid);
    alien.setName(aname);
    repo.save(alien);
    return "home.jsp";
}

and

    @RequestMapping("/get")
public ModelAndView get(@RequestParam("text") String text)
{
    ModelAndView mv = new ModelAndView();
    int id = Integer.parseInt(text);
    mv.setViewName("show.jsp");
    mv.addObject("alien", repo.findById(id));
    return mv;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文