Невозможно выполнить проверки для страницы входа в систему с помощью Struts.Здесь я храню данные для входа, введенные пользователем в базу данных.Я не хочу хранить нулевые значения в базе данных.Но в следующей программе, если я отправляю форму без ввода данных, она дает исключение .. проверки не генерируются .. Пожалуйста, предложите мне.Здесь я публикую свои программы.
Заранее спасибо
LoginFormBean.java
------------
package info.strutspkg;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.DynaValidatorForm;
public class LoginFormBean extends DynaValidatorForm
{
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
}
LoginActionClass.java
------------
package info.strutspkg;
import java.sql.Connection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import login.logindao.Logindao;
import org.apache.struts.action.*;
import org.apache.struts.validator.DynaValidatorForm;
public class LoginActionClass extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception
{
Connection con=null;
String key=null;
try{
DynaValidatorForm loginForm=(DynaValidatorForm)form;
String uname=loginForm.get("username").toString();
String pwd=loginForm.getString("password").toString();
Logindao dao=new Logindao();
int i=dao.insert(uname,pwd);
System.out.println("r value returned");
if(i>0){
key="success";
}
else
{
key="failure";
}
}
catch(Exception e){
e.printStackTrace();
System.out.println("Error occured");
}
return mapping.findForward(key);
}
}
Logindao.java
--------
package login.logindao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Logindao {
public int insert(String uname,String pwd){
int r=0;
PreparedStatement ps = null;
Connection conn=null;
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=sample;user=sa;password=lalitha";
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//Get a connection
conn = DriverManager.getConnection(url);
}
catch (Exception except)
{
except.printStackTrace();
}
//get connection object from DBConnection class
System.out.println("Connection"+conn);
try{
//use PreparedStatement to insert the values of username and password
ps=conn.prepareStatement("insert into login(username,password) values(?,?)");
ps.setString(1, uname);
ps.setString(2, pwd);
r = ps.executeUpdate();
}
catch(SQLException ex) {
try {
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
System.err.println("SQLException: " + ex.getMessage());
}
System.out.println("r="+r);
return r;
}
}
ApplicationResources.properties
-----------
logindetails.username= username
logindetails.password= password
struts-config.xml
--------
<form-beans>
<form-bean name="logindetails" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="username" type="java.lang.String"></form-property>
<form-property name="password" type="java.lang.String"></form-property>
</form-bean>
</form-beans>
<action path="/register" name="logindetails" type="info.strutspkg.LoginActionClass" scope="request" validate="true">
<forward name="success" path="/success.jsp"/>
<forward name="failure" path="/failure.jsp"/>
</action>
</action-mappings>
<message-resources parameter="info.strutspkg.ApplicationResources" />
<!-- Validator plugin -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
validation.xml
<form name="logindetails">
<field
property="username"
depends="required">
<arg key="logindetails.username"/>
</field>
<field
property="password"
depends="required">
<arg key="logindetails.password"/>
</field>
</form>
</formset>
Error:
------
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: The path of an ForwardConfig cannot be null
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
root cause
java.lang.IllegalArgumentException: The path of an ForwardConfig cannot be null
org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:70)
org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)