1》首先是页面的addpath.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>添加路线</title> <mce:script language="javascript" src="<%=request.getContextPath()%><!-- /scripts/jquery-1.4.3.min.js"> // --></mce:script> <link rel="stylesheet" href="<%=request.getContextPath()%>/styles/table.css" type="text/css" /> <mce:script type="text/javascript"><!-- $(function(){ $("select[name='startstationid']").change(function(){ if($(this).val()!="/u9009择起点站"){//选择起点站,如果选择项不是“选择起点站”,使用jquery的ajax,url为selectstationforpath.do $.ajax({ type:"post", url:"selectstationforpath.do", data:{ startstationid : $(this).val() }, success:function(data, textStatus){ $("select[name='endstationid']").html(data); }, error: function(){ alert("/u6ca1有站点可以选择!"); } }); }else{ $("select[name='endstationid']").html("<option>/u9009择终点站</option>"); } }); $("select[name='endstationid']").change(function(){ if($("select[name='startstationid']").val()=="/u9009择起点站"){//如果选择项是“选择起点站”,提示选择 alert("/u8bf7选择起点站!"); }else if($(this).val() == "/u9009择终点站"){//如果选择项是“选择终点站”,提示选择 alert("/u8bf7选择终点站!"); }else{ $.ajax({ type:"post", url:"judgeexistenceofpath.do", data:{ startstationid : $("select[name='startstationid']").val(), endstationid : $(this).val() }, success:function(data, textStatus){ if(data == 1){ alert("/u8def线已经存在,请重新选择!"); } }, error: function(){ alert("/u6ca1有路劲可以选择!"); } }); }; }); }) // --></mce:script> </head> <body> <form id="pathsform" method="post" action="addPath.do" name="path"> <table> <thead> <tr> <th colspan="2">添加路线</th> </tr> </thead> <tbody> <!-- private int id; private Station startstation; private Station endstation; private int distance; private String remark; private String description; --> <tr> <td class="label">起始站点:</td> <td class="content2"><select name="startstationid"> <option>选择起点站</option> <c:forEach var="station" items="${paths_stations}"> <option value="${station.id}" label="${station.stationName}"> </c:forEach> </select></td> </tr> <tr> <td class="label">直达站点:</td> <td class="content2"><select name="endstationid"> <option>选择终点站</option> <!-- <c:forEach var="station" items="${paths_stations}"> <option value="${station.id}" label="${station.stationName}"> </c:forEach> --> </select></td> </tr> <tr> <td class="label">是否直达:</td> <td class="content2"> <input type="radio" name="through" value="是">是 <input type="radio" name="through" value="否">否 </td> </tr> <tr> <td class="label">站间距离:</td> <td class="content2"><input type="text" class="required" name="distance"/> km</td> </tr> <tr> <td class="label">备注:</td> <td class="content2"><input type="text" name="remark" class="required"/></td> </tr> </tbody> <tfoot> <tr> <td colspan="2" class="buttons"><input value="提交" type="submit"> <input value="取消" type="reset"></td> </tr> </tfoot> </table> </form> </body> </html>
2》然后是struts的配置文件struts-config.xml
<!--选择起站点的action--> <action path="/selectstationforpath" type="cn.osunda.logistics.action.stationmanage.StationAction" scope="request" parameter="selectStationForPathAction"> </action> <!--判断路线是否存在的action--> <action path="/judgeexistenceofpath" type="cn.osunda.logistics.action.stationmanage.StationAction" scope="request" parameter="judgeExistenceOfPathAction"> </action>
3》最后是action的编写
public ActionForward selectStationForPathAction(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { try { String startstationid = request.getParameter("startstationid"); List station = sdi.getObjectsByProperty(Station.class, "id", "<>", Integer.parseInt(startstationid));//这是我后台写的公共接口 response.setContentType("text/html;charset=utf-8");//设置字符集 PrintWriter out = response.getWriter();//out.println输出的内容就是前台jquerysuccess:function(data, textStatus)中对应的data if (station != null && station.size() != 0) { for (int i = 0; i < station.size(); i++) { out.println("<option value='" + ((Station) station.get(i)).getId() + "' label='" + ((Station) station.get(i)).getStationName() + "'>"); } } else { out.println("<option value='未选择' label='没有可选路线'>"); } out.close(); return null; } catch (Exception ex) { ex.printStackTrace(); return mapping.getInputForward(); } } public ActionForward judgeExistenceOfPathAction(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { try { String startstationid = request.getParameter("startstationid"); String endstationid = request.getParameter("endstationid"); Map<String, Object> map = new HashMap<String, Object>(); map.put("startstation", Integer.parseInt(startstationid)); map.put("endstation", Integer.parseInt(endstationid)); Path path = (Path) pdi.getSingleByPropertys(Path.class, map);//这是我后台写的公共接口 response.setContentType("text/html;charset=utf-8");//设置字符集 PrintWriter out = response.getWriter();//out.println输出的内容就是前台jquerysuccess:function(data, textStatus)中对应的data if (path != null) { out.println(1); } else { out.println(0); } out.close(); return null; } catch (Exception ex) { ex.printStackTrace(); return mapping.getInputForward(); } }
4》页面预览图例