注册操作返回请求的资源不可用

问题描述 投票:0回答:2

我有一个包含以下代码的表单:

<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>

我的错误报告是:

  • 类型状态报告
  • 留言 /JAthenaeum/doRegister
  • 描述 请求的资源不可用。

为什么我会遇到这个问题?

forms jsp struts
2个回答
0
投票

问题在于表单中的 action 参数:在我的例子中它必须是

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>

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" >
© www.soinside.com 2019 - 2024. All rights reserved.