簡體   English   中英

我是 spring MVC 的新手。使用 Maven java 以 spring MVC 形式獲取空值作為輸出。 如何從文件中獲取值到我的 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

我是 spring MVC 的新手。使用 Maven java 以 spring MVC 形式獲取空值作為輸出。 如何從文件中獲取值到我的 jsp。 請幫助擺脫這種情況。

JSP創建如下

<%@ 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>

這是我的控制器文件,是完成請求映射的地方。

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

    }

}

我在單獨的文件中完成了 getter 和 setter,其中完成了 getter 和 setter

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”方法中返回模型而不是字符串。這樣,您就可以從響應中獲取值。

如果您正在執行 POST,我假設您使用正文調用它,在這種情況下,您需要將標簽@RequestBody添加到將映射正文的參數中,在這種情況下,我認為是Employee

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

據我所知,您在控制器中所做的基本上是為您的 Employee 類創建一個新對象,然后嘗試從中訪問值。 因此它們始終為空。 我建議使用帶有模型屬性的 mvc 表單。 在您當前的程序中,而不是從控制器中的 Employee 訪問值,創建一個 HttpservletRequest 對象,然后從中訪問值。

或者,您可能試圖在控制器中使用 @RequestBody。 希望它有所幫助並將bean放入您的配置文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM