gpt4 book ai didi

java - 我是 spring MVC 的新手。使用 Maven java 在 spring MVC 表单中获取 null 值作为输出。如何从文件中获取值到我的jsp

转载 作者:行者123 更新时间:2023-12-01 18:35:15 24 4
gpt4 key购买 nike

我是 spring MVC 的新手。使用 Maven java 在 spring MVC 表单中获取 null 值作为输出。如何从文件中获取值到我的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>

这是我的 Controller 文件,是完成请求映射的地方。

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”方法中返回模型而不是字符串。这样,您就可以从响应中获取值。

关于java - 我是 spring MVC 的新手。使用 Maven java 在 spring MVC 表单中获取 null 值作为输出。如何从文件中获取值到我的jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60072933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com