jsf commandButtonmanagedBean操作重定向到404页面

时间:2018-10-18 16:15:57

标签: spring jsf managed-bean

我有我的ManagedBean TodoService.java

flagsCut = flags[flags['Area'] < 6000]
flagsCut = flagsCut[flagsCut['Population'] < 250]

我有表格要添加新数据 index.xhtml

flagsCut = flags[(flags['Area'] < 6000) | (flags['Population'] < 250)]
plt.scatter(flagsCut['Area'], flagsCut['Population'], 'b', alpha=0.5)
plt.xlabel('Area')
plt.ylabel('Population')
plt.show()

当我执行添加按钮时,我将被重定向到状态为404的同一页面。 有我的web.xml

package com.medkhelifi.tutorials.todolist.services;

import com.medkhelifi.tutorials.todolist.models.dao.ITodoDao;
import com.medkhelifi.tutorials.todolist.models.entities.Todo;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

@ManagedBean
@RequestScoped
public class TodoService implements ITodoService {

    @ManagedProperty(value = "ITodoDao")
    private ITodoDao ITodoDao;

    @Override
    public void addTodo(Todo todo) {
        ITodoDao.addTodo(todo);
    }

    public void setITodoDao(ITodoDao ITodoDao) {
        this.ITodoDao = ITodoDao;
    }
}

我也使用Spring安全性: applicationContext-security.xml

   <!-- extra code coes here -->     
   <b:column colMd="6">
            <div class="todolist not-done">
                <h1>Todos</h1>
                <h:form>
                    <b:inputText type="text" class="form-control add-todo" placeholder="Todo Title"/>
                    <b:inputTextarea type="text" placeholder="Description" />
                    <b:dateTimePicker placeholder="todo Date" format="YYYY-MM-DD HH:mm:ss"/>
                    <h:commandButton action="#{todoService.addTodo(null)}" class="btn btn-success" value="Add"/>
                </h:form>

                <hr/>
                <ul class="list-unstyled" >
                    <ui:repeat value="#{TodoDao.getCurrentUserTodos()}" var="todo"  >
                        <h:panelGroup rendered="#{!todo.done}">
                            <li class="ui-state-default">
                                <div class="checkbox">
                                    <label><input type="checkbox" value="" />#{todo.title}</label>
                                </div>
                            </li>
                        </h:panelGroup>
                    </ui:repeat>
                </ul>
                <div class="todo-footer">
                    <strong><span class="count-todos"/></strong> Items Left
                </div>
            </div>
        </b:column>
  <!-- extra code coes here -->

当我调试时,从未调用过TodoService.addTodo方法,我希望我能很好地解释我的问题。 有我的堆栈: 春季4.2.2-决赛 JSF 2.2.17

1 个答案:

答案 0 :(得分:0)

经过几个小时的搜索,我发现了这个问题: 我错过了在表单中添加csrf安全隐藏输入的功能,因为我在Spring安全性中使用了csrf保护。

index.xhtml

   <!-- extra code coes here -->     
   <b:column colMd="6">
            <div class="todolist not-done">
                <h1>Todos</h1>
                <h:form>
                    <b:inputText type="text" class="form-control add-todo" placeholder="Todo Title"/>
                    <b:inputTextarea type="text" placeholder="Description" />
                    <b:dateTimePicker placeholder="todo Date" format="YYYY-MM-DD HH:mm:ss"/>
                    <!-- I missed to add this line -->
                    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
                    <h:commandButton action="#{todoService.addTodo(null)}" class="btn btn-success" value="Add"/>
                </h:form>

                <hr/>
                <ul class="list-unstyled" >
                    <ui:repeat value="#{TodoDao.getCurrentUserTodos()}" var="todo"  >
                        <h:panelGroup rendered="#{!todo.done}">
                            <li class="ui-state-default">
                                <div class="checkbox">
                                    <label><input type="checkbox" value="" />#{todo.title}</label>
                                </div>
                            </li>
                        </h:panelGroup>
                    </ui:repeat>
                </ul>
                <div class="todo-footer">
                    <strong><span class="count-todos"/></strong> Items Left
                </div>
            </div>
        </b:column>
  <!-- extra code coes here -->