我已经将JSF2.0用于自动导航系统。我有一个简单的登录页面,当我点击登录按钮时,相应Bean类的方法已经调用,但当我将扩展名从.jsp更改为xhtml时,它不会导航到home.jsp页面,然后它就工作了。.jsp扩展有什么问题。
LoginBean.java
public class LoginBean implements Serializable {
private static final long serialVersionUID = 1L;
private String uname;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String loginProject() {
System.out.println("hello i am called ");
System.out.println(uname);
System.out.println(password);
return "home";
}
}
================
login.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Colonial Inn</title>
</h:head>
<h:body>
<div align="center">
<h:form id="loginForm">
<p:growl id="msg" showDetail="true" life="3000" />
<p:panel header="Login" style="width: 360px,margin-left:200px;">
<h:panelGrid id="loginPanel" columns="2">
<h:outputText value="Username" />
<p:inputText id="username" value="#{loginBean.uname}"
required="true">
</p:inputText>
<p:spacer></p:spacer>
<p:message for="username"></p:message>
<h:outputText value="Password" />
<p:password id="password" value="#{loginBean.password}"
feedback="false" required="true"></p:password>
<p:spacer></p:spacer>
<p:message for="password"></p:message>
<p:spacer></p:spacer>
<h:commandButton action="#{loginBean.loginProject}" value="Login"></h:commandButton>
</h:panelGrid>
</p:panel>
</h:form>
</div>
</h:body>
</html>
===================
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>NewCalonialinn</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
====================
home.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Insert title here</title>
</head>
<body>
<h1>Welcome to home page </h1>
</body>
</html>
.jsp扩展有什么问题
自JSF 2.0以来,它一直被弃用。
只需使用.xhtml扩展即可。
另请参阅:
- 为什么从JSF2.0开始,Facelets优先于JSP作为视图定义语言