Home > other >  Form Action in JSP not able to call different methods
Form Action in JSP not able to call different methods

Time:04-15

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?

CodePudding user response:

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;
}

CodePudding user response:

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>
  • Related