gpt4 book ai didi

mysql - 使用 hibernate 注释从 jsp 中的下拉列表获取值到 spring mvc Controller

转载 作者:行者123 更新时间:2023-11-30 22:58:27 26 4
gpt4 key购买 nike

您好,我正在编写一个程序,使用 spring mvc hibernate 和 mysql 作为数据库。我在 mysql 数据库中有两个表 employeeteam。我正在使用 jsp 作为我的 View .在我的 jsp 中,我将团队名称从团队表加载到下拉列表中。当我从下拉列表中选择特定团队并单击提交按钮时,我想显示特定团队中的所有员工

这是我的jsp

<%@page import="java.sql.ResultSet"%>
<%@page import="com.mysql.jdbc.PreparedStatement"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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:form method="POST" action="Search.html" modelAttribute="command">

<table>
<tr>
<form:label path="teams.teamId">Team Name</form:label>

<td>
<form:select path="teams.teamId" cssStyle="width: 150px;">
<option value="-1">Select a type</option>
<c:forEach items="${teamKey}" var="teams">
<option value="${teams.teamId}">${teams.teamName}</option>
</c:forEach>
</form:select>
</td>
</tr>

<tr>



<td colspan="2"><input type="submit" value="submit"> </td>

</tr>



</table>

</form:form>
<h1><a href="retunTohome.html">Home</a></h1>
</body>
</html>

我的 jsp View

enter image description here

Controller 代码

@RequestMapping(value="/Search",method = RequestMethod.POST)
public ModelAndView fromSearch(@ModelAttribute("command") Resource resource,BindingResult result){
return new ModelAndView("redirect:/searchResult.html");
}

@RequestMapping(value="/searchResult",method = RequestMethod.GET)
public String getValue(@ModelAttribute("command") Resource resource ,Model m){


m.addAttribute("command", "Got value here"+resource.toString());
return "Search";
}

资源类

   @Entity
@Table(name="Employee")
public class Resource implements Serializable {

private static final long serialVersionUID = -723583058586873479L;


@Id
@Column(name="empid")
private Integer empId;

@Column(name="empname")
private String empName;

@Column(name="empexp")
private Integer empExp;


@Column(name="empskills")
private String empSkills;



@ManyToOne
@JoinColumn(name="teamId",referencedColumnName="teamId")
private AddTeam teams;



public Integer getEmpId(){
return empId;
}
public void setEmpId(Integer empId){
this.empId=empId;
}


public String getEmpName(){
return empName;
}
public void setEmpName(String empName){
this.empName=empName;
}

public Integer getEmpExp(){
return empExp;
}
public void setEmpExp(Integer empExp){
this.empExp=empExp;
}


public String getEmpSkills(){
return empSkills;
}
public void setEmpSkills(String empSkills){
this.empSkills=empSkills;
}



public AddTeam getTeams() {
return teams;
}

public void setTeams(AddTeam teams) {
this.teams = teams;
}

}


AddTeam.class

@Entity
@Table(name="Team")

public class AddTeam {


@Id
@Column(name="teamId")
private Integer teamId;

@Column(name="teamName")
private String teamName;

public Integer getTeamId() {
return teamId;
}

public void setTeamId(Integer teamId) {
this.teamId = teamId;
}

public String getTeamName() {
return teamName;
}

public void setTeamName(String teamName) {
this.teamName = teamName;
}

}

ResourceDao 实现类

@Override
public void serchResources(int teamid) {
// TODO Auto-generated method stub
sessionfactory.getCurrentSession().createQuery(" FROM Resource WHERE teamId=" +teamid);


}

错误

org.springframework.beans.NotReadablePropertyException: Invalid property 'teams' of bean class [java.lang.String]: Bean property 'teams' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:665)
org.springframework.beans.BeanWrapperImpl.getNestedBeanWrapper(BeanWrapperImpl.java:518)
org.springframework.beans.BeanWrapperImpl.getBeanWrapperForPropertyPath(BeanWrapperImpl.java:495)
org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:655)
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:147)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129)
org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119)
org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
org.apache.jsp.WEB_002dINF.views.Search_jsp._jspx_meth_form_005flabel_005f0(Search_jsp.java:306)
org.apache.jsp.WEB_002dINF.views.Search_jsp._jspService(Search_jsp.java:117)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我是 spring hibernate 应用程序的新手,我被困在这里。有什么方法可以显示所选团队的员工......

感谢任何建议...

最佳答案

您的代码将难以维护:

  • 你调用Resource可以叫什么Employee类(表名是Employee)
  • 你调用AddTeam可以叫什么Team类(表名是Team)
  • 你调用teams (复数)单个 AddTeam元素
  • 你使用一个完整的Resource反对简单地组建一个团队
  • 所有这些都没有评论

我想您只是想在该 jsp 中建立一个团队,并且该表单正确发送了一个 teamId。它可以那样工作,多亏了 spring magic,但是你需要声明一个 Converter<String, AddTeam>将浏览器发送的字符串(teamId 的表示)转换为具有该 ID 的 AddTeam。

要让它在 spring 中正确集成,您必须:

  • 声明FormattingConversionServiceFactoryBean bean(如果不这样做,您将丢失所有默认转换)
  • 声明Converter<String, AddTeam> bean用于转换String -> Integer -> AddTeam 并注册到上面的ConversionService

在您的表单中,选择的路径应该仅为 teams :

<form:select path="teams" ...

这是一个相当高级的配置,但它可以工作。


如果您喜欢不那么神奇的东西,请使用真实形式(而不是模型对象):

class TeamCommand {
private int teamId;
// getter and setter ommitted for bievety
}

在你的 Controller 中:

@RequestMapping(value="/Search",method = RequestMethod.POST)
public ModelAndView fromSearch(@ModelAttribute("command") TeamCommand command,BindingResult result){
AddTeam team = service.method(command.getTeamId);
...
return new ModelAndView("redirect:/searchResult.html");
}

在你的 JSP 中:

<form:select path="teamId" ...>

关于mysql - 使用 hibernate 注释从 jsp 中的下拉列表获取值到 spring mvc Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25176352/

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