小弟我第一次发帖,希望各位大大帮忙解答,在此先谢谢~~~
我跟随书中的例子做helloworld,现在页面打开没有问题,就是点击页面上的提交按钮,页面只是被刷新了一下,无变化。在debug模式下对这个提交方法打断点但是没进去。说白了就是页面里这个提交按钮没有调用到我写的sayHello方法,请问这是什么原因呢?小弟在线等~下面贴出我的代码:
页面HelloWorld.xhtml:
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:rich="http://richfaces.org/rich"
template="layout/template.xhtml">
<ui:define name="body">
<h2>
Seam Hello World.
</h2>
<form>
<p>Please enter your name:</p>
<p><h:inputText value="#{person.name}" size="15"/></p>
<h:commandButton type="submit" value="say Hello" action="#{manager.sayHello()}" />
</form>
<f:subview id="fans" rendered="#{!empty(fans)}">
<p>The following fans have said "hello" to JBoss Seam:</p>
<h:dataTable value="#{fans}" var="fan">
<h:column>
<h:outputText value="#{fan.name}" />
</h:column>
</h:dataTable>
</f:subview>
</ui:define>
</ui:composition>
用到的ACTION方法ManagerAction.java:
package book.helloseamgen;
import java.util.List;
import static org.jboss.seam.ScopeType.SESSION;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;
//@Stateless
@Scope(SESSION)
@Name("manager")
public class ManagerAction implements Manager{
@In@Out
private Person person;
@Out
private List <Person>fans;
@In
private FacesMessages facessMessages;
// @PersistenceContext
// private EntityManager em;
public String sayHello() {
try{
String xx = "sssss";
// em.persist(person);
// person = new Person();
// fans = em.createQuery("select p from Person p").getResultList();
System.out.println("xx = " + xx);
return null;
}
catch(Exception e){
facessMessages.add("error");
return null;
}
}
@Out
public Person getPerson() {
return person;
}
@In
public void setPerson(Person person) {
this.person = person;
}
@Out
public List<Person> getFans() {
return fans;
}
}
------解决方案--------------------------------------------------------
sayHello方法的返回值表示提交按钮后要跳转的画面。
你返回了null.所以只刷新当前画面。
可以参考
http://docs.jboss.org/seam/2.0.0.GA/reference/zh/html/tutorial#registration-example
的【1.2.1.2. 无状态会话Bean:RegisterAction.java】