这里我使用这个link创建了动态下拉列表,但是当我从可用列表中选择一些值时,应该在动作类中调用它。
可以在图像中看到的下拉列表,这里的值是从数据库动态加载的,现在我想要的是当我从那两个下拉列表中选择值时(我的意思是文本值)的任何值应该被发送到动作class和那里我将在这两个值的基础上执行一个JDBC select查询,并将显示在图像中显示的表中但是一切都应该在load.Action应该是从下拉列表中选择值而不是任何按钮点击.With静态值我能够将值从下拉列表调用到具有name
属性的操作类。但在这种情况下我不能:(
我希望我现在很清楚。
我尝试使用listkey,name和id调用select标签但是没有一个工作 下面是我的JSP代码:
<div>
<div class="invoicetext1">Event Name :</div>
<s:select name="dp.eventState"
list="%{state}"
class="billlistbox1"
id="eventName" />
<div>
<s:select name="dp.companyState"
class="billlistbox2"
listKey="companyState"
list="%{status}">
</s:select>
</div>
<div class="invoicetext2">Company Name :</div>
<div class="clear"></div>
</div>
<s:form action="ActionSelect">
<s:submit value=" Click Here"/>
</s:form>
<div>
用于加载动态下拉列表的操作类:
package com.ca.actions;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.ca.database.Database;
import com.ca.pojo.Event;
import java.sql.PreparedStatement;
import com.opensymphony.xwork2.ActionSupport;
public class RetrieveEvNaCoNaAction extends ActionSupport {
private static final long serialVersionUID = -5418233715172672477L;
List<Event> dataForBillsJspList;
private List state = new ArrayList();
private List status = new ArrayList();
String eventName;
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public RetrieveEvNaCoNaAction() {
// TODO Auto-generated constructor stub
}
public List<Event> getDataForBillsJspList() {
return dataForBillsJspList;
}
public void setDataForBillsJspList(List<Event> dataForBillsJspList) {
this.dataForBillsJspList = dataForBillsJspList;
}
public List getStatus() {
return status;
}
public void setStatus(List status) {
try {
Database database = new Database();
Connection con = database.Get_Connection();
PreparedStatement ps = con
.prepareStatement("SELECT EVENT_NAME,COMPANY_NAME,date_format(FROM_DATE,'%d/%m/%Y') as dateAsFrom,date_format(TO_DATE,'%d/%m/%Y') as dateAsTo FROM EVENT");
ResultSet rs = ps.executeQuery();
//dataForBillsJspList = new ArrayList<Event>();
while (rs.next()) {
/*dataForBillsJspList.add(new Event(rs.getString("EVENT_NAME"),
rs.getString("COMPANY_NAME"), rs
.getString("dateAsFrom"), rs
.getString("dateAsTo")));
System.out.println(rs.getString("EVENT_NAME"));*/
status.add(rs.getString("COMPANY_NAME"));
}
System.out.println("Data Collected ...");
}catch(Exception e)
{
e.printStackTrace();
}
}
public List getState() {
return state;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
setState(this.state);
setStatus(this.status);
return "success";
}
public String showEventDetails(){
System.out.println("Hi.."+eventName);
return SUCCESS;
}
public void setState(List state) {
//implement the application specific logic to
try {
Database database = new Database();
Connection con = database.Get_Connection();
PreparedStatement ps = con
.prepareStatement("SELECT EVENT_ID,EVENT_NAME,COMPANY_NAME,CONTACT_PERSON,CONTACT_NO,EMAIL_ID,EVENT_VENUE,date_format(FROM_DATE,'%d/%m/%Y') as dateAsFrom,date_format(TO_DATE,'%d/%m/%Y') as dateAsTo ,EVENT_TIME FROM EVENT");
ResultSet rs = ps.executeQuery();
dataForBillsJspList = new ArrayList<Event>();
while (rs.next()) {
dataForBillsJspList.add(new Event(rs.getString("EVENT_ID"),rs.getString("EVENT_NAME"),
rs.getString("COMPANY_NAME"),rs.getString("CONTACT_PERSON"),rs.getString("CONTACT_NO"),rs.getString("EMAIL_ID"),rs.getString("EVENT_VENUE"), rs
.getString("dateAsFrom"), rs
.getString("dateAsTo"),rs.getString("EVENT_TIME")));
//System.out.println(rs.getString("EVENT_NAME"));
state.add(rs.getString("EVENT_NAME"));
System.out.println(rs.getString("EVENT_ID"));
}
System.out.println("Data Collected ...");
}catch(Exception e)
{
e.printStackTrace();
}
//Here for displaying the data on UI, we are using few hardcoded values//
}
}
现在加载动态下拉列表后,我试图通过S.O.P调用动作类中的选定值,但它会给出空指针异常。以下是我的POJO课程:
package com.ca.pojo;
public class Dropdown
{
private String eventState;
private String companyState;
public Dropdown() {
// TODO Auto-generated constructor stub
}
public String getEventState() {
return eventState;
}
public void setEventState(String eventState) {
this.eventState = eventState;
}
public String getCompanyState() {
return companyState;
}
public void setCompanyState(String companyState) {
this.companyState = companyState;
}
}
及以下是动作类,我试图通过使用name属性调用该选定值:
package com.ca.actions;
import com.ca.pojo.Dropdown;
import com.opensymphony.xwork2.ActionSupport;
public class DropdownAction extends ActionSupport
{
Dropdown dp;
public DropdownAction() {
// TODO Auto-generated constructor stub
}
public Dropdown getDp() {
return dp;
}
public void setDp(Dropdown dp) {
this.dp = dp;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println(dp.getEventState());
return "success";
}
}
struts.xml已正确配置。现在选择了两个值之后我想在下表中显示数据而没有任何按钮点击但是在jsp中我创建了按钮,只是为了看我是否在动作类中获得所选值但实际上我想要它而没有任何按钮点击。
答案 0 :(得分:0)
使用Javascript / jQuery可以做到这一点,这取决于你到达动作类后想要做什么。
如果您想导航到其他页面,请使用以下代码。
将onchange
事件作为属性添加到您的下拉列表中
onchange="customFunction(this.value)"
在标题部分
中创建customFunction
function customFunction(selectedValue){
window.location="Action_URL?myValue="+selectedValue;
}
或者,如果您想返回同一页面,请使用jQuery ajax,
$("#eventName").change(function(e){
var selectedValue = $(this).val();
$.ajax({
type : 'post',
url : 'Action_URL',
data: { myValue: selectedValue},
success : function(data) {
alert(data);
console.log(data);
}
});
});
希望这有帮助。
答案 1 :(得分:0)
嗯,这里有一个很大的混乱:D
首先,抛出NullPointerException,因为未发送值,并且由于它们不在表单中,因此不会发送值。
您应该将它们包含在这样的表单中,以便将它们发送到ActionSelect
操作:
<s:form action="ActionSelect">
<div class="invoicetext1">Event Name :</div>
<s:select name="dp.eventState"
list="%{state}"
class="billlistbox1"
id="eventName" />
<div>
<s:select name="dp.companyState"
class="billlistbox2"
listKey="companyState"
list="%{status}">
</s:select>
</div>
<div class="invoicetext2">Company Name :</div>
<div class="clear"></div>
</div>
<s:submit value=" Click Here"/>
</s:form>
解决了这个问题,但这并没有解决你的问题。
您可以通过两种主要方式联系页面中的操作:
使用标准提交(正如您所做):
您要么提交包含其内容的表单,要么通过最终在查询字符串中传递参数来调用链接。这将创建一个Request,它将联系一个将返回整个JSP的动作,该动作将被加载以代替您现在所在的页面。
使用AJAX:
您在不更改当前页面的情况下POST或GET到操作,并且操作可以返回任何内容,例如JSP片段,JSON结果,二进制结果(通过Struts2 Stream结果)等...
然后,您可以选择如何处理返回的数据,例如将其加载到之前为空的<div>
内,或者具有不同的内容。
现在您的问题是,您正在联系的行动不是您来自的行动(无法重新渲染您正在使用的整个JSP),而是在不使用AJAX的情况下调用它,然后无论映射到"success"
结果的对象是什么(整个JSP,还是JSP片段),它都将被加载以代替您所在的JSP,并且它将失败。
由于你似乎对此很陌生,我建议你从简单的解决方案开始(没有AJAX),并且在熟悉它之后,下次尝试使用AJAX。
那就是说,
setState
,setStatus
...); prepare()
方法中加载选择框内容等信息,以便在出现错误时也可用; 以下是对代码的重构,以实现目标。我只会将@Getter
和@Setter
用于语法糖(它们是龙目岛注释,但你继续使用你的getter和setter,这只是为了清晰起见):
<head>
<script>
$(function(){
$("#event, #company").on('change',function(){
$("#myForm").submit();
});
});
</script>
</head>
<body>
<form id="myForm">
<div>
...
<s:select id="event" name="event" list="events" />
...
<s:select id="company" name="company" list="companies" />
...
</div>
</form>
<div>
...
Table - iterate **dataForBillsJspList** here
...
</div>
</body>
public class RetrieveEvNaCoNaAction extends ActionSupport {
private static final long serialVersionUID = -5418233715172672477L;
@Getter private List<Event> dataForBillsJspList = new ArrayList<Event>();
@Getter private List<String> events = new ArrayList<String>();
@Getter private List<String> companies = new ArrayList<String>();
@Getter @Setter private String event = null;
@Getter @Setter private String company = null;
@Override
public void prepare() throws Exception {
Connection con;
try {
con = new Database().Get_Connection();
// load companies
PreparedStatement ps = con.prepareStatement("SELECT DISTINCT company_name FROM event");
ResultSet rs = ps.executeQuery();
while (rs.next()) { companies.add(rs.getString("company_name")); }
// load events
ps = con.prepareStatement("SELECT DISTINCT event_name FROM event");
rs = ps.executeQuery();
while (rs.next()) { events.add(rs.getString("event_name")); }
} catch(Exception e) {
e.printStackTrace();
} finally {
con.close();
}
}
@Override
public String execute() {
Connection con;
try {
con = new Database().Get_Connection();
// load the table. The first time the table is loaded completely
String sql = "SELECT EVENT_ID, EVENT_NAME, COMPANY_NAME, CONTACT_PERSON, CONTACT_NO, EMAIL_ID, EVENT_VENUE, " +
"date_format(FROM_DATE,'%d/%m/%Y') as dateAsFrom, date_format(TO_DATE,'%d/%m/%Y') as dateAsTo ,EVENT_TIME " +
"FROM event";
String where = "";
// if instead this action has been called from the JSP page,
// the result is filtered on event and company:
if (event!=null && company!=null) {
where = " WHERE event_name = ? AND company_name = ?";
}
// load companies
PreparedStatement ps = con.prepareStatement(sql + where);
if (where.length()>0) {
ps.setString(1,event);
ps.setString(2,company);
}
ResultSet rs = ps.executeQuery();
while (rs.next()) {
dataForBillsJspList.add(new Event(rs.getString("EVENT_ID"),rs.getString("EVENT_NAME"),rs.getString("COMPANY_NAME"),
rs.getString("CONTACT_PERSON"),rs.getString("CONTACT_NO"),rs.getString("EMAIL_ID"),
rs.getString("EVENT_VENUE"), rs.getString("dateAsFrom"), rs.getString("dateAsTo"),
rs.getString("EVENT_TIME")));
}
} catch(Exception e) {
e.printStackTrace();
} finally {
con.close();
}
return SUCCESS;
}
}
这是一个启动的例子,但它应该有用。
接下来的步骤是: