我有一个包含以下代码的表单:
<form action="doRegister" class="form-signup" >
<h2 class="form-signup-heading">Please sign up</h2>
<input type="email" class="form-control" placeholder="Email address" required autofocus>
<input type="password" class="form-control" placeholder="Password" required>
<input type="password" class="form-control" placeholder="Password control" required>
<input type="text" class="form-control" placeholder="Name" required>
<input type="text" class="form-control" placeholder="Surname" required>
<input type="date" class="form-control" placeholder="Born date" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign up</button>
</form
我有两个课程:UserRegisterForm
和UserRegistrationAction
UserRegisterForm
package com.github.jcpp.jathenaeum.action;
import org.apache.struts.action.ActionForm;
public class UserRegisterForm extends ActionForm{
private static final long serialVersionUID = 1;
/*ATTRIBUTES of the form fields*/
/*METHODS Get and Set*/
public UserRegisterForm() {
super();
}
}
UserRegistrationAction
package com.github.jcpp.jathenaeum.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.github.jcpp.jathenaeum.Utente;
import com.github.jcpp.jathenaeum.db.dao.UtenteDAO;
import com.github.jcpp.jathenaeum.exceptions.RegistrationException;
public class UserRegistrationAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//boolean action_perform = false;
String action_target = null;
Random rnd = new Random();
UtenteDAO userDao = new UtenteDAO();
Utente user = new Utente();
//ActionMessages errors_mesg = new ActionMessages();
UserRegisterForm uf = (UserRegisterForm) form;
if(form != null){
user.setEmail(uf.getEmail());
user.setPassword(uf.getPassword());
user.setNome(uf.getName());
user.setCognome(uf.getSurname());
user.setDataNascita(uf.getBornDate());
user.setNumeroTessera(rnd.nextInt(999999)+1);
try{
if(userDao.register(user)){
action_target = "success";
}
}catch(Exception e){
action_target = "failed";
throw new RegistrationException();
}
}
return mapping.findForward(action_target);
}
在我的struts-config.xml
我:
<form-beans>
<form-bean name="registerform" type="com.github.jcpp.jathenaeum.action.UserRegisterForm"/>
</form-beans>
<action-mappings>
<action path="/index" type="org.apache.struts.actions.ForwardAction" parameter="page.index" />
<action path="/signin" type="org.apache.struts.actions.ForwardAction" parameter="page.signin" />
<action path="/signup" type="org.apache.struts.actions.ForwardAction" parameter="page.signup" />
<action
path="/doRegister"
type="com.github.jcpp.jathenaeum.action.UserRegistrationAction"
name="registerform"
scope="request"
validate="true"
input="signup">
<forward name="input" path="/index.jsp"/>
<forward name="success" path="/welcome.jsp"/>
<forward name="failure" path="/index.jsp"/>
</action>
</action-mappings>
我的错误报告是:
为什么我会遇到这个问题?
答案 0 :(得分:0)
该操作会返回名为"failed"
的未知转发。将其更改为配置为操作"failure"
的配置。以下代码应替换
try {
if(!userDao.register(user)) {
return mapping.findForward("failure");
}
}catch(Exception e){
throw new RegistrationException(e);
}
return mapping.findForward("success");
异常应提供更多信息,告诉您异常的原因。
此外,表单应映射到操作权
<html:form action="/doRegister" cssClass="form-signup" >
答案 1 :(得分:0)
问题在于表单中的操作参数:在我的情况下它必须是doRegister.do
,在struts-config.xml
中,操作路径等于/doRegister
。因此,请参阅以下代码:
<form action="doRegister.do" >
[...]
</form>
和struts-config.xml
<action
path="/doRegister" <!-- LOOK HERE -->
type="com.github.jcpp.jathenaeum.action.UserRegistrationAction"
name="registerform"
scope="request"
validate="true"
input="/signup.jsp">
<!-- <forward name="input" path="/index.jsp"/>
<forward name="success" path="/signin.jsp"/>
<forward name="failed" path="/signup.jsp"/> -->
</action>