gpt4 book ai didi

java - Spring mvc 选择按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:26:52 24 4
gpt4 key购买 nike

我想创建带有一些填充值的简单 html 选择标签(现在我输入的内容,稍后从数据库中获取)。这是我的 Controller :

@Controller
public class HomeController {

@RequestMapping("/test")

public String home(){
return "index";
}

@ModelAttribute("items")
public ArrayList<String> getList(){
ArrayList<String> itemList = new ArrayList<String>();
itemList.add("one");
itemList.add("two");
itemList.add("three");
return itemList;
}

}

在我的索引部分,这里是相关的 HTML 代码:

<div class="form-row">
<label for="items">Items:</label>
<span class="input"><form:select path="items" items="${items}" itemLabel="name" /></span>
</div>

这产生(这被渲染成 html):

<div class="form-row">

<label for="items">Items:</label>

<span class="input"><form:select path="items" items="[one, two, three]" itemLabel="name" /></span>

</div>

而且我还有这样的想法(我期望产生的东西):

<div class="form-row">

<label for="items">Items:</label>

<span class="input"><select name="name">
<option>one</option>
<option>two</option>
<option>three</option>
</select></span>

</div>

我可以通过添加 <option> 来改变我的 getList() 方法之前和</option>在 arrayList 中的每个项目之前,我确信有一种更好的方法可以做到这一点,这就是我在这里问的原因,请记住我才刚刚开始学习 spring MVC。谢谢

问题更新:

当我根据 skaffman 的建议编辑它时,添加了这个:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

之后

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

它导致了这个异常:

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/index.jsp at line 39

36: Date of birth: <input type="text" size="10" />
37: <div class="form-row">
38: <label for="items">Items:</label>
39: <span class="input"><form:select path="items" items="${items}" itemLabel="name" /></span>
40: </div>
41: </fieldset>
42:


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:498)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
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:1063)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:801)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:195)
org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:159)
org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:141)
org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:90)
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:417)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
root cause

org.springframework.beans.NotReadablePropertyException: Invalid property 'name' of bean class [java.lang.String]: Bean property 'name' 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:702)
org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:694)
org.springframework.web.servlet.tags.form.OptionWriter.doRenderFromCollection(OptionWriter.java:210)
org.springframework.web.servlet.tags.form.OptionWriter.renderFromCollection(OptionWriter.java:180)
org.springframework.web.servlet.tags.form.OptionWriter.writeOptions(OptionWriter.java:133)
org.springframework.web.servlet.tags.form.SelectTag.writeTagContent(SelectTag.java:216)
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.index_jsp._jspx_meth_form_005fselect_005f0(index_jsp.java:206)
org.apache.jsp.WEB_002dINF.views.index_jsp._jspService(index_jsp.java:109)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
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:1063)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:801)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:195)
org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:159)
org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:141)
org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:90)
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:417)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

最佳答案

这是你的错误:

Invalid property 'name' of bean class [java.lang.String]: Bean property 'name' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

你得到这个是因为你告诉标签使用 name生成 <option> 时列表中每个项目的属性标签。但是您的列表由字符串组成,没有 name属性(property)。

只需将其从标签中删除,就可以了,即:

<form:select path="items" items="${items}" />

关于java - Spring mvc 选择按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4657563/

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