在spring mvc应用程序JSP页面中,我试图通过为用户提供一个下拉列表来简化布局,从中可以选择所需的操作。下拉列表中的项目将分别对应于用户将指向的其他JSP的不同URL。这将使用户界面比为每个可能的URL单独按钮更清晰。如何在代码中设置它?
以下是Spring Expression Language中的jsp代码,用于创建到目前为止的下拉列表:
<form:select path="AddLink" cssClass="textArea">
<option value="Select" label=" - Select - " ></option>
<form:options items="${AddLinks}"/>
</form:select>
以下是控制器中的方法,用于填充下拉列表,然后处理我到目前为止的选择:
@RequestMapping(value = "/encounters", method = RequestMethod.GET)
public String processFindForm(@RequestParam("encounterID") String encounterId, Encounter encounter, BindingResult result, Map<String, Object> model) {
int encountId = Integer.parseInt(encounterId);
Encounter sel_encounter = this.clinicService.findEncounterById(encountId);//I added this
HashMap<String,String> AddLinks = new HashMap<String,String>();
AddLinks.put("New this","/thislink");
AddLinks.put("New that","/thatlink");
AddLinks.put("New other thing","/otherlink");
model.put("sel_encounter",sel_encounter);
((Model) model).addAttribute("AddLinks", AddLinks);
return "encounters/encountersList";
}
@RequestMapping(value = "/encounters", method = RequestMethod.POST)
public String processFindForm(@ModelAttribute("encounter") Encounter encounter, @ModelAttribute("AddLink") String addLink, BindingResult result, SessionStatus status) {
int encountId = encounter.getId();
System.out.println("............ addLink is: "+addLink);
return "/"+addLink;
}
这导致以下堆栈跟踪:
org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/encounters/encountersList.jsp at line 116
113: <spring:url value="" var="medications_newUrl"></spring:url>
114: <a href="${fn:escapeXml(medications_newUrl)}" class="btn btn-info" >Add New Medication</a>
115:
116: <form:select path="AddLink" cssClass="textArea">
117: <option value="Select" label=" - Select - " ></option>
118: <form:options items="${AddLinks}"/>
119: </form:select>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)
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:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
com.github.dandelion.datatables.core.web.filter.DatatablesFilter.doFilter(DatatablesFilter.java:73)
com.github.dandelion.datatables.extras.servlet2.filter.DatatablesFilter.doFilter(DatatablesFilter.java:71)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'AddLink' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:179)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:165)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:152)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:143)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:127)
org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421)
org.springframework.web.servlet.tags.form.SelectTag.writeTagContent(SelectTag.java:200)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:103)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
org.apache.jsp.WEB_002dINF.jsp.encounters.encountersList_jsp._jspx_meth_form_005fselect_005f0(encountersList_jsp.java:1396)
org.apache.jsp.WEB_002dINF.jsp.encounters.encountersList_jsp._jspService(encountersList_jsp.java:405)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
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:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
com.github.dandelion.datatables.core.web.filter.DatatablesFilter.doFilter(DatatablesFilter.java:73)
com.github.dandelion.datatables.extras.servlet2.filter.DatatablesFilter.doFilter(DatatablesFilter.java:71)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
作为参考,这里是JSP顶部包含的taglib:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="datatables" uri="http://github.com/dandelion/datatables" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="petclinic" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="joda" uri="http://www.joda.org/joda/time/tags" %>
注意:我认为问题在于使用atModelAttribute
将字符串传递给控制器方法。我认为错误意味着当我想要的时候Spring正在寻找一个对象AddLink传递一个字符串。这有助于您了解解决方案吗?
通过将所有AddLink
引用更改为AddLinks
,我使用链接选项的下拉列表编译JSP,但选择任何选项都没有效果。 JSP不重定向,System.out.println()
控制器方法中的POST
不打印任何内容,表明尚未调用POST
控制器方法。
以下是我更改的唯一代码:
// in the JSP
<form:select path="AddLinks" cssClass="textArea">
<option value="Select" label=" - Select - " ></option>
<form:options items="${AddLinks}"/>
</form:select>
// in the controller
@RequestMapping(value = "/encounters", method = RequestMethod.POST)
public String processFindForm(@ModelAttribute("encounter") Encounter encounter, @ModelAttribute("AddLink") String AddLinks, BindingResult result, SessionStatus status) {
int encountId = encounter.getId();
System.out.println("............ addLink is: "+AddLinks);
return "/"+AddLinks;
}
可以在文件共享网站at this link上找到完整的JSP。
答案 0 :(得分:1)
类似于@looser我也会争辩说你最好在客户端使用javascript进行编码,但无论如何这是服务器端的答案:
首先我想你忘了model.addAttribute("AddLinks", AddLinks)
。我可以看到你正在创建一个永远不会被添加到模型中的HashMap。
其次在processFindForm
中,您可以返回字符串“redirect:/ blah”,将HTTP 3xx重定向到/blah
URI。重定向URL与您的上下文路径相关。
答案 1 :(得分:1)
试试这个,
修改您的Encounter类以保存selectedLink的值,如:
public class Encounter {
private String selectedLink;
//getters and setters
}
然后,在控制器中添加AddLinks选项作为modelAttribute,如下所示:
public String getFindForm(Model model){
model.addAttribute("enctr",new Encounter());
return "AddLink";
}
@RequestMapping(method=RequestMethod.POST)
public String processFindForm(@ModelAttribute("enctr")Encounter encounter){
System.out.println(encounter.getSelectedLink());
return "AddLink";//"redirect:"+encounter.getSelectedLink();
}
@ModelAttribute("addLinkOptions")
public Map loadLinkOptions(){
LinkedHashMap<String, String> addLinks = new LinkedHashMap<String,String>();
addLinks.put("/thislink","New this");
addLinks.put("/thatlink","New that");
addLinks.put("/otherlink","New other thing");
return addLinks;
}
并在你的jsp中添加一个spring form:form标签来执行操作。
<form:form method="post" modelAttribute="enctr">
SelectLink:
<form:select path="selectedLink">
<form:option value="NONE">--select--</form:option>
<form:options items="${addLinkOptions}"/>
</form:select>
<input type="submit"/>
</form:form>
答案 2 :(得分:0)
好的我不熟悉Spring MVC但是在你的要求中
onChange or onSelect event is enough
<form:select path="AddLink" cssClass="textArea" onChange='setAction()' >
<option value="Select" label=" - Select - " ></option>
<form:options items="${AddLinks}"/>
</form:select>
In javaScript
<script type="text/javascript">
function setAction()
{
var temp = document.getElementById("selectTagID");
var action = temp.options[temp.selectedIndex].value;//get Selected option key value
document.form_name.action =action;//Send the action
document.form_name.submit();//Submit the form
}
</script>
您的Otions列表中的哪些位置可以使这些操作可用..作为所选选项的键