简体   繁体   中英

I am new to spring MVC .Getting null vales as output in spring MVC form using Maven java. how to fetch the values from the files to my jsp

I am new to spring MVC .Getting null vales as output in spring MVC form using Maven java. how to fetch the values from the files to my jsp. Please help to get out out of this.

JSP created as below

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//w3c//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="../store" method="post">
        Eno:<input type="text name="eno"/>
        Name:<input type="text name="name"/>
        Address:<input type="text name="address"/>
        ContactNumber:<input type="text name="contact"/>
        EmailId:<input type="text name="email"/>
        Salary:<input type="text name="salary"/> 
        <input type="submit"
            value="store" />
    </form>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</body>
</html>

This is my controller file is where request mapping is done.

package webapp1;

import javax.servlet.ServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(path="/store")
public class EmployeeController {
    @RequestMapping(method=RequestMethod.POST)
    public String saveEmployee(Employee employee,Model model) {
        System.out.println("eno:"+ employee.getEno()+"\n");
        System.out.println("name:"+ employee.getName()+"\n");
        System.out.println("address:"+ employee.getAddress()+"\n");
        System.out.println("contact:"+ employee.getContact()+"\n");
        System.out.println("email:"+ employee.getEmail()+"\n");
        System.out.println("salary:"+ employee.getSalary()+"\n");
        model.addAttribute("employee",employee);
         return "display";

    }

}

I have done getter and setter in separate file where getter and setter is done

package webapp1;

public class Employee {
    private Integer eno;
    private String name;
    private String address;
    private Double contact;
    private String email;
    private Double salary;
public  Employee() {
            }

    public Integer getEno() {
        return eno;
    }
    public void setEno(Integer eno) {
        this.eno = eno;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public Double getContact() {
        return contact;
    }
    public void setContact(Double contact) {
        this.contact = contact;
    }

    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Double getSalary() {
        return salary;
    }
    public void setSalary(Double salary) {
        this.salary = salary;
    }

    public Employee(Integer eno, String name, String address, Double contact, String email, Double salary) {
        super();
        this.eno = eno;
        this.name = name;
        this.address = address;
        this.contact = contact;
        this.email = email;
        this.salary = salary;
    }



}

您可能需要在“saveEmployee”方法中返回模型而不是字符串。这样,您就可以从响应中获取值。

If you are doing a POST I assume you are calling it with a body, in that case you need to add the tag @RequestBody to the param that will map the body, in this case I think is the Employee .

public String saveEmployee(@RequestBody Employee employee, Model model) {
    ...
}

From what I can understand, What you are doing in your controller is essentially making a new object of your Employee class and then trying to access the values from it. Therefor they are always null. I would recommend using mvc form with model attributes. In your current program rather than accessing values from Employee in your controller, Make a HttpservletRequest object and then access the values from it.

Or maybe you were trying to use @RequestBody in the controller. Hope it helps and put beans in your configuration file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM