我正在使用MVC框架,在jsp页面上我有两个选项(拾取/丢弃)列表中的(pickdate / picktime)和(dropdate / droptime)作为文本字段,我试图插入(pickdate / picktime)和(dropdate / droptime)根据列表框中的选择,如果用户选择拾取然后(pickdate / picktime)被插入数据库,反之亦然,但它不起作用,请告诉我我错在哪里。
<jsp:useBean id="MrBean" class="pack.java.adminmodel"/>
<jsp:setProperty name ="MrBean" property="employeid" param="sid"/>
<jsp:setProperty name ="MrBean" property="supervisor" param="ssup"/>
<jsp:setProperty name ="MrBean" property="department" param="sdept"/>
<jsp:setProperty name ="MrBean" property="list" param="mylist"/>
<jsp:setProperty name ="MrBean" property="pickdate" param="pte"/>
<jsp:setProperty name ="MrBean" property="picktime" param="ptm"/>
<jsp:setProperty name ="MrBean" property="dropdate" param="dte"/>
<jsp:setProperty name ="MrBean" property="droptime" param="dtm"/>
<%@ taglib uri="/WEB-INF/jsp2/taglib2.tld" prefix="easy" %>
<html>
<body >
<form method = "post">
<H1>Please enter the following details below.</H1>
<table border="1" align="left" cellpadding ="30" cellspacing="5">
<tr>
<td align="left">
Employid <input type="text" name="sid" />
Supervisor <input type="text" name="ssup" />
Department <input type="text" name="sdept" />
<label>Select your option</label>
<select id="myList" name="mylist">
<option value="1" >Pickup</option>
<option value="2" >Drop</option>
</select>
Pickup date <input type="date" name="pte" />
Pickup time <input type="time" name="ptm" /></br>
Drop date <input type="date" name="dte" />
Drop time <input type="time" name="dtm" /></br>
<input type="submit" name="submit" value="submit" /></br>
</td>
</tr>
</table>
<table border="1" align="right" cellpadding ="30" cellspacing="2">
<tr>
<td align="left">
<a href="myprojectallrequest.jsp">View all requests</a></br>
<a href="myprojectallrequest.jsp">View pending requests</a>
</td>
</tr>
</table>
<%
String aa=request.getParameter("submit");
if(aa!=null)
{%>
<easy:myTag1/>
<%
}
%>
</form>
</body>
</html>
这是一个模型类。
package pack.java;
import java.io.*;
import java.lang.*;
public class adminmodel
{
public static Integer employeid;
public static String supervisor;
public static String department;
public static String list;
public static String pickdate;
public static String picktime;
public static String dropdate;
public static String droptime;
public void setEmployeid ( Integer emp)
{
this.employeid=emp;
}
public Integer getEmployeid()
{
return employeid;
}
public void setSupervisor (String s)
{
this.supervisor=s;
}
public String getSupervisor()
{
return supervisor;
}
public void setDepartment (String d)
{
this.department=d;
}
public String getDepartment()
{
return department;
}
public void setList (String pp)
{
this.list=pp;
}
public String getList()
{
return list;
}
public void setPickdate (String p)
{
this.pickdate=p;
}
public String getPickdate()
{
return pickdate;
}
public void setPicktime (String pt)
{
this.picktime=pt;
}
public String getPicktime()
{
return picktime;
}
public void setDropdate (String dd)
{
this.dropdate=dd;
}
public String getDropdate()
{
return dropdate;
}
public void setDroptime (String dt)
{
this.droptime=dt;
}
public String getDroptime()
{
return droptime;
}
}
这是一个控制器类
package pack.java;
import pack.java.adminmodel;
import java.io.*;
import java.lang.*;
import java.sql.*;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class admincontrol extends TagSupport
{
HttpServletRequest request;
HttpServletResponse response;
public int doStartTag() throws JspException
{
request=(HttpServletRequest)pageContext.getRequest();
response=(HttpServletResponse)pageContext.getResponse();
return EVAL_PAGE;
}
public void check ()
{
JspWriter out=pageContext.getOut();
Connection con;
Statement stmt;
ResultSet rs;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ex)
{
}
if (adminmodel.list.equals("pickup"))
{
try
{
con= DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:XE","gaurav","oracle");
stmt=con.createStatement();
rs=stmt.executeQuery("insert into myadmin(employeid,supervisor,department,pickdate,picktime) values('"+adminmodel.employeid+"','"+adminmodel.supervisor+"','"+adminmodel.department+"','"+adminmodel.pickdate+"','"+adminmodel.picktime+"') ");
}
catch(SQLException ex)
{
}
}
else if (adminmodel.list.equals("drop"))
try
{
con= DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:XE","gaurav","oracle");
stmt=con.createStatement();
rs=stmt.executeQuery("insert intomyadmin(employeid,supervisor,department,dropdate,droptime) values('"+adminmodel.employeid+"','"+adminmodel.supervisor+"','"+adminmodel.department+"','"+adminmodel.dropdate+"','"+adminmodel.droptime+"') ");
}
catch(SQLException ex)
{
}
}
public int doEndTag() throws JspException
{
check();
return super.doEndTag();
}
}
答案 0 :(得分:0)
最明显的错误是您将字符串与==
进行比较。 ==
比较引用,而不是值。即它测试两个String对象是否相同。如果它们包含相同的字符则不会。使用.equals()
比较字符串。
另一个大问题是你的“控制器”是一个JSP标签实现。这毫无意义。标记用于从JSP生成一些HTML标记。因此它是视图的一部分(MVC中的V)。控制器应该实现为servlet或servlet调用的POJO。
看起来你正在重新发明轮子并使用你自己开发的MVC框架。不要那样做。特别是如果你从Java开始。使用基本的servlet和JSP,(servlet作为控制器,JSP作为视图),或使用着名的MVC框架,如Spring MVC或Stripes。条纹非常易于使用,如果你正在开始设置它。