h:jsf2中的commandLink未调用bean方法,甚至未通过javascript调用
这是在将我的项目迁移到tomcat7上的jsf2之后发生的。早些时候,针对JSF1的tomcat5.5运行良好。我有一个.xhtml文件,我试图从中通过h:commandLink调用托管bean方法,但它没有被调用。我已尝试添加与同一主题相关的其他stackoverflow论坛中建议的EL 2.2 JAR,并在web.xml中添加了条目:h:jsf2中的commandLink未调用bean方法,甚至未通过javascript调用,javascript,jsf-2,Javascript,Jsf 2,这是在将我的项目迁移到tomcat7上的jsf2之后发生的。早些时候,针对JSF1的tomcat5.5运行良好。我有一个.xhtml文件,我试图从中通过h:commandLink调用托管bean方法,但它没有被调用。我已尝试添加与同一主题相关的其他stackoverflow论坛中建议的EL 2.2 JAR,并在web.xml中添加了条目: <context-param> <param-name>org.apache.myfaces.EXPRESSION_FACTO
<context-param>
<param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
<param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
org.apache.myfaces.EXPRESSION\u工厂
com.sun.el.ExpressionFactoryImpl
com.sun.faces.expressionFactory
com.sun.el.ExpressionFactoryImpl
但这个问题并没有得到解决。请帮忙
.xhtml文件:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:t="http://myfaces.apache.org/tomahawk">
<f:view>
<f:loadBundle var="text" basename="#{basePage.bundleName}"/>
<title>#{text['user.passwordHint']}</title>
<p>Looking up password hint for ${param.username}...</p>
<h:form id="passwordForm">
<h:inputHidden id="username" value="#{passwordHint.username}"/>
<h:commandLink action="#{passworHint.execute}" id="execute">
<f:param name="username" value="${param.username}"></f:param>
</h:commandLink>
</h:form>
<script type="text/javascript">
var f = document.forms['passwordForm'];
f.elements['passwordForm:_link_hidden_'].value='passwordForm:execute';
f.elements['username'].value='${param.username}';
f.submit();
</script>
</f:view>
</html>
#{text['user.passwordHint']}
正在查找${param.username}的密码提示
var f=document.forms['passwordForm'];
f、 元素['passwordForm:\u link\u hidden'].value='passwordForm:execute';
f、 元素['username'].value='${param.username}';
f、 提交();
托管bean:
public class PasswordHint extends BasePage {
@ManagedProperty(value="#{param.username}")
private String username;
/* private String execute;
public String getExecute() {
return execute;
}
public void setExecute(String execute) {
this.execute = execute;
}*/
public String getUsername() {
System.out.println("get username of passwordhint-------"+username);
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String execute() {
/* FacesContext context = FacesContext.getCurrentInstance();
Map<String,String> params = context.getExternalContext().getRequestParameterMap();
System.out.println(params.get("username"));
System.out.println("Inside password hint execute-------------");
*/
// ensure that the username has been sent
if (username == null || "".equals(username)) {
log.warn("Username not specified, notifying user that it's a required field.");
addError("errors.required", getText("user.username"));
return null;
}
if (log.isDebugEnabled()) {
log.debug("Processing Password Hint...");
}
// look up the user's information
try {
User user = userManager.getUserByUsername(username);
System.out.println("username retrieved---------"+username);
StringBuffer msg = new StringBuffer();
msg.append("Your password hint is: " + user.getPasswordHint());
msg.append("\n\nLogin at: " + RequestUtil.getAppURL(getRequest()));
message.setTo(user.getEmail());
String subject = '[' + getText("webapp.name") + "] " + getText("user.passwordHint");
message.setSubject(subject);
message.setText(msg.toString());
mailEngine.send(message);
addMessage("login.passwordHint.sent",
new Object[] { username, user.getEmail() });
} catch (Exception e) {
e.printStackTrace();
System.out.println("In exception----------------");
// If exception is expected do not rethrow
//addError("login.passwordHint.error", username);
addMessage("login.passwordHint.sent", username);
}
return "success";
}
faces-config.xml:
<navigation-rule>
<from-view-id>/passwordHint.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/login.jsp</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>passwordHint</managed-bean-name>
<managed-bean-class>com.webapp.action.PasswordHint</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>username</property-name>
<value>#{param.username}</value>
</managed-property>
<managed-property>
<property-name>userManager</property-name>
<value>#{userManager}</value>
</managed-property>
<managed-property>
<property-name>mailEngine</property-name>
<value>#{mailEngine}</value>
</managed-property>
<managed-property>
<property-name>message</property-name>
<value>#{mailMessage}</value>
</managed-property>
<managed-property>
<property-name>templateName</property-name>
<value>accountCreated.vm</value>
</managed-property>
</managed-bean>
公共类密码提示扩展了BasePage{
@ManagedProperty(value=“#{param.username}”)
私有字符串用户名;
/*私有字符串执行;
公共字符串getExecute(){
返回执行;
}
公共void setExecute(字符串执行){
this.execute=execute;
}*/
公共字符串getUsername(){
System.out.println(“获取密码提示的用户名----”+用户名);
返回用户名;
}
public void setUsername(字符串用户名){
this.username=用户名;
}
公共字符串execute(){
/*FacesContext context=FacesContext.getCurrentInstance();
Map params=context.getExternalContext().getRequestParameterMap();
System.out.println(参数get(“用户名”);
System.out.println(“内部密码提示执行----------------”);
*/
//确保已发送用户名
如果(用户名==null | |“”.equals(用户名)){
警告(“未指定用户名,通知用户它是必填字段。”);
addError(“errors.required”,getText(“user.username”);
返回null;
}
if(log.isDebugEnabled()){
调试(“正在处理密码提示…”);
}
//查找用户的信息
试一试{
User=userManager.getUserByUsername(用户名);
System.out.println(“检索到的用户名---------”+用户名);
StringBuffer msg=新的StringBuffer();
append(“您的密码提示是:”+user.getPasswordHint());
msg.append(“\n\n登录:”+RequestUtil.getAppURL(getRequest());
message.setTo(user.getEmail());
字符串subject='['+getText(“webapp.name”)+“]”+getText(“user.passwordHint”);
message.setSubject(主题);
message.setText(msg.toString());
发送(消息);
addMessage(“login.passwordHint.sent”,
新对象[]{username,user.getEmail()});
}捕获(例外e){
e、 printStackTrace();
System.out.println(“在例外情况下-----------------”;
//如果预期出现异常,请不要重新调用
//addError(“login.passwordHint.error”,用户名);
addMessage(“login.passwordHint.sent”,用户名);
}
返回“成功”;
}
faces-config.xml:
/passwordHint.xhtml
成功
/login.jsp
密码提示
com.webapp.action.PasswordHint
要求
用户名
#{param.username}
用户管理器
#{userManager}
邮件引擎
#{mailnengine}
消息
#{mailMessage}
模板名
accountCreated.vm
是的,这是我正在尝试的实际代码,这是复制粘贴实际代码时的输入错误。javascript错误还显示:f.elements['passwordForm:\u link\u hidden']为空或不是对象。
您的页面上没有h:head
和h:body
。您的commandLink操作中的密码提示拼写不正确。这是您正在尝试的实际代码吗?