繁体   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