我尝试使用" jsp:include"来定义JSP页面作为我的标签的内容。 - 喜欢这个(注:" page0.jsp") ......
<ul id="smTabs" class="nav nav-tabs" style="margin-bottom: 15px;">
<li class="active">
<a href="/#page0" data-toggle="tab">Page0</a>
</li>
-
-
-
<div id="smTabContent" class="tab-content">
<div class="tab-pane fade active in" id="page0">
<jsp:include page="page0.jsp" />
</div>
问题:
当用户点击标签时,&#34;包含&#34; (即&#34; jsp:include&#34;)JSP页面表单字段为空,因为控制器尚未初始化。
问题:
什么&#34; href =&#34;值将成功显示选项卡内容(例如,page0.jsp),并初始化控制器,以便页面正确显示数据?
感谢您的帮助。
以下是page0.jsp的空形式...
以下是它的样子......
以下是有趣的额外信息......
这是tabbedPage.jsp
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<br/><br/><br/>
<div class="page-header"><h1>Tabbed Page Management</h1></div>
<div class="bs-component">
<ul id="smTabs" class="nav nav-tabs" style="margin-bottom: 15px;">
<li class="active">
<a href="/#page0" data-toggle="tab">Page0</a>
</li>
<li>
<a href="/#page1" data-toggle="tab">Page1</a>
</li>
<li>
<a href="/#page2" data-toggle="tab">Page2</a>
</li>
</ul>
<div id="smTabContent" class="tab-content">
<div class="tab-pane fade active in" id="page0">
<h4>${page0FormBean}</h4>
<jsp:include page="page0.jsp" />
</div>
<div class="tab-pane fade" id="page1">
<h4>${page1FormBean}</h4>
<jsp:include page="page1.jsp" />
</div>
<div class="tab-pane fade" id="page2">
<h4>${page2FormBean}</h4>
<jsp:include page="page2.jsp" />
</div>
</div>
</div>
这里是&#34; tabbedPage.jsp&#34; ...:TabbedPageController.java
的控制器 import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@Scope("session")
public class TabbedPageController
{
private static final Logger LOG = Logger.getLogger("TabbedPageController");
@RequestMapping(value = {"/tabbedPage"}, method = RequestMethod.GET)
public String tabbedPage(ModelMap model, HttpSession session)
{
LOG.info("_________________________tabbedPage_________________________entering...");
return "tabbedPage";
}
}
这是page0.jsp(其中一个JSP页面包括......,通过&#34; jsp:include&#34;)
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<br/><br/><br/>
<div class='panel panel-primary'>
<div class='panel-heading'>
<h2 class='panel-title'>Page0</h2>
</div>
</div>
<form:form id="page0Form" modelAttribute="page0FormBean" method="post">
<div class="form-group row-fluid">
<label class='col-xs-6 control-label' for='formFieldA'>Page0's Form Field A</label>
<div class='col-xs-6'>
<input class='form-control' type='text' id='formFieldA' value='${page0FormBean.formFieldA}' />
</div>
</div>
<div class="form-group row-fluid">
<label class='col-xs-6 control-label' for='formFieldB'>Page0's Form Field B</label>
<div class='col-xs-6'>
<input class='form-control' type='text' id='formFieldB' value='${page0FormBean.formFieldB}' />
</div>
</div>
<div class="form-group row-fluid">
<label class='col-xs-6 control-label' for='formFieldC'>Page0's Form Field C</label>
<div class='col-xs-6'>
<input class='form-control' type='text' id='formFieldC' value='${page0FormBean.formFieldC}' />
</div>
</div>
</form:form>
这里是&#34; page0.jsp&#34; ...:Page0Controller.java
的控制器 import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.apache.log4j.Logger;
import java.io.Serializable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
@Controller
@Scope("session")
public class Page0Controller implements Serializable
{
private static final Logger LOG = Logger.getLogger("Page0Controller");
private static final long serialVersionUID = 1602123387257827883L;
@RequestMapping(value = "/page0", method = RequestMethod.GET)
public ModelAndView page0(ModelMap model)
{
LOG.info("____________________page0___________________entering...");
Page0FormBean page0FormBean = new Page0FormBean("page0fieldavalue", "page0fieldbvalue", "page0fieldcvalue");
model.addAttribute("page0FormBean", page0FormBean);
return new ModelAndView("tabbedPage", "page0FormBean", page0FormBean);
}
}
这是&#34;表单bean&#34;由&#34; page0.jsp&#34; ...:Page0FormBean.java
使用 public class Page0FormBean
{
private String formFieldA;
private String formFieldB;
private String formFieldC;
public Page0FormBean(String formFieldA, String formFieldB, String formFieldC)
{
this.formFieldA = formFieldA;
this.formFieldB = formFieldB;
this.formFieldC = formFieldC;
}
public String getFormFieldA()
{
return formFieldA;
}
public void setFormFieldA(String formFieldA)
{
this.formFieldA = formFieldA;
}
public String getFormFieldB()
{
return formFieldB;
}
public void setFormFieldB(String formFieldB)
{
this.formFieldB = formFieldB;
}
public String getFormFieldC()
{
return formFieldC;
}
public void setFormFieldC(String formFieldC)
{
this.formFieldC = formFieldC;
}
@Override
public String toString()
{
return "Page0FormBean{" + "formFieldA=" + formFieldA + ", formFieldB=" + formFieldB + ", formFieldC=" + formFieldC + '}';
}
}
答案 0 :(得分:0)
我现在已经找到了解决方案。 - 希望如果他们有更好的方法,Spring MVC专家将会加入进来
基本上,我使用的是会话范围变量:“tabno”。 - 我在JSP页面中使用“tabno”来控制活动选项卡等(注意:在EL中使用三元表达式)
而不是href=#page0
,而是使用href=page1
来调用page0.jsp的控制器。
-Page0Controller然后将控制传递给TabbedPageController,page0.jsp在tabbedPage.jsp中显示为“tab content”。
现在可以使用。
由于
标签JSP代码如下所示.. 。
-
-
-
<div class="bs-component">
<ul id="smTabs" class="nav nav-tabs" style="margin-bottom: 15px;"> <%-- <%pageContext.forward("page1.jsp");%> --%>
<li class="${sessionScope.tabno==0?'active':''}" >
<a href="page0">Page0</a>
</li>
<li class="${sessionScope.tabno==1?'active':''}" >
<a href="page1">Page1</a>
</li>
<li class="${sessionScope.tabno==2?'active':''}" >
<a href="page2">Page2</a>
</li>
</ul>
<div id="smTabContent" class="tab-content">
<div class="${sessionScope.tabno==0?'tab-pane fade active in':'tab-pane fade'}" id="page0">
<jsp:include page="page0.jsp" />
</div>
<div class="${sessionScope.tabno==1?'tab-pane fade active in':'tab-pane fade'}" id="page1">
<jsp:include page="page1.jsp" />
</div>
<div class="${sessionScope.tabno==2?'tab-pane fade active in':'tab-pane fade'}" id="page2">
<jsp:include page="page2.jsp" />
</div>
</div>
</div>
-
-
-
Page0Controller.java(与page0.jsp相关联)看起来像这样...... (您可以看到我在会话属性中设置“tabno”值的位置)...
@Controller
@Scope("session")
public class Page0Controller implements Serializable
{
private static final Logger LOG = Logger.getLogger("Page0Controller");
private static final long serialVersionUID = 1602123387257827883L;
@RequestMapping(value = "/page0", method = RequestMethod.GET)
public ModelAndView page0(ModelMap model, HttpSession session)
{
Page0FormBean page0FormBean = new Page0FormBean("page0fieldavalue", "page0fieldbvalue", "page0fieldcvalue");
model.addAttribute("page0FormBean", page0FormBean); //<== setting the "modelAttribute" that's used in page.jsp...
session.setAttribute("tabno", "0"); //<== setting "tabno" value here...
return new ModelAndView("tabbedPage", "page0FormBean", page0FormBean);
}
}
TabbedPageController.java(与tabbedPage.jsp相关联)看起来像这样......
@Controller
@Scope("session")
public class TabbedPageController implements Serializable
{
private static final Logger LOG = Logger.getLogger("TabbedPageController");
private static final long serialVersionUID = 6570072463879652843L;
@RequestMapping(value = {"/tabbedPage"}, method = RequestMethod.GET)
public String tabbedPage(ModelMap model, HttpSession session)
{
return "tabbedPage";
}
}