第一个JSF程序

实验课为开成绩证明迟到了,还好助教又演示了一遍。。

1. Severs

简单说来就是new一个server,选择Tomcat v7.0 Server,下一步,把要在这个server里运行的程序都加进去。

完成以后,双击这个server,可以检查下server location是否正确,还可以查看ports,再检查下modules。

关于ports,有时候莫名冲突。解决的办法是:

(1)在server视图下双击某个server,可以查看port,也可以直接修改!!!当然,你也能在Servers工程下找到对应的tomcat的server.xml文件修改。

(2)找到你apache tomcat文件夹位置,进入conf文件夹,修改server.xml,找到Connector元素的port属性,可能像这样:<Connector  port="8080" protocol="HTTP/1.1" .../>  顺序不一定哦,自己找仔细啦。把port值改掉,重新new一个server即可。

(3)打开cmd窗口,输入"netstat -ano",可以发现你哪些端口被哪些进程占用了,记下相应的PID值。再打开window任务管理器,在进程这栏,查看-选择列-勾选显示PID,如图所示,干掉那个占用你端口的进程就可以了。——感谢日光浴同学……

扫描二维码关注公众号,回复: 657335 查看本文章

2.Dynamic Web Project

new一个dynamic web project,target runtime选择Apache Tomcat v7.0,configuration选择JavaServer Faces v2.0 Project,下一步,下一步,在Web Module这勾选generate web.xml deployment descriptor,下一步,在Type这选择Disabled Library Configuration,最后finish。

从FTP上拷贝下jsf用的jar包,在加进jstl.jar,把这8个jar包拖进你的工程下的WebContent/WEB-INF/lib里,这样,你在Java Resource/Libraries/Apache Tomcat v7.0下就会发现很多新jar包了。

一个动态网页工程至此配置完成。

为了更方便地使用呢,你还可以在菜单栏-window-show view-other-palatte。以后当你使用Web Page Editor打开jsp文件(p.s. 新建JSP文件时选择的template是JavaServer Faces(JSF)Page (html)。)时,就是Palatte大显神通的时候了(faces-config.xml的Navigation Rule模式下它也很有用哦)。

让jsp默认地使用Web Page Editor打开的设置是:preferrences-查找File Assosiation-选择*.jsp-下栏选择Web Page Editor并单击Default-OK。不要默认的话就右击jsp文件,open with-Web Page Editor。

效果如图:

3.编写代码

只提下faces-config.xml和web.xml。

(1)faces-config.xml的ManagedBean模式下,可以add你定制的bean;在Navigation Rule模式下,可以把相应的jsp文件拖进来,从Palatte栏选择link工具,把两个jsp文件链接起来,再select这条线,在properties的from outcome里填上信息,这个信息就是你的bean对跳转页面的反应字符串(我去= = 我根本没明白这个机制)。如图:

(2)web.xml,你会发现run以后http://localhost:8080/你的工程名/ 根本是不显示的,纯粹是web.xml的url-pattern! 因此还要加上faces/hello.jsp这样的字样。默认的欢迎主页呢,你也可以改成faces/hello.jsp,这样一run就是你的主页了。如图:

重要问题!!!

1、刚开始run老是蹦错,提示:javax.servlet.ServletException: #{helloBean.saygoodbye }: javax.faces.el.MethodNotFoundException: saygoodbye : org.jia.hello.HelloBean.saygoodbye ()

javax.faces.webapp.FacesServlet.service(FacesServlet.java:209)

百思不得其解,求问百度,发现尼玛啊!!!原来是#{helloBean.saygoodbye }这里方法名和}不能有空格啊!!!!!而一半打出#{后自动就给你加空格和}啊!!!!第一次遇上这么极品的问题

2、JSP是放在webContent文件夹下…… 不要再愚蠢了地球人

=========================================================================

源码:

HelloBean.java

package org.jia.hello;

public class HelloBean {
	private int number;

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}
	public String saygoodbye(){
		if(number>100)
			return "goodbye";
		else return "newfile";
	}
}

hello.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
	<f:view>
		<h:form>
			<h:inputText id="inputText1" value="#{helloBean.number}">
			</h:inputText>
			<h:commandButton value="Say goodbye" type="submit"
				action="#{helloBean.saygoodbye}" /> <!--新:这里不要有"immediate=true"!!-->
		</h:form>

	</f:view>
</body>
</html>

goodbye.jsp

<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<f:view>
good bye!<h:outputText value="#{helloBean.numer}"/><!--新-->
</f:view>
</body>
</html>

NewFile.jsp

<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<f:view>
new file!<h:outputText value="#{helloBean.numer}"/><!--新-->
</f:view>
</body>
</html>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
	<managed-bean>
		<managed-bean-name>helloBean</managed-bean-name>
		<managed-bean-class>org.jia.hello.HelloBean</managed-bean-class>
		<managed-bean-scope>session</managed-bean-scope>
	</managed-bean>
	<navigation-rule>
		<display-name>hello</display-name>
		<from-view-id>/hello.jsp</from-view-id>
		<navigation-case>
			<from-outcome>goodbye</from-outcome>
			<to-view-id>/goodbye.jsp</to-view-id>
		</navigation-case>
	</navigation-rule>
	<navigation-rule>
		<display-name>hello</display-name>
		<from-view-id>/hello.jsp</from-view-id>
		<navigation-case>
			<from-outcome>newfile</from-outcome>
			<to-view-id>/NewFile.jsp</to-view-id>
		</navigation-case>
	</navigation-rule>

</faces-config>

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>Test</display-name>
  <welcome-file-list>
    <welcome-file>faces/hello.jsp</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>/faces/*</url-pattern>
  </servlet-mapping>
</web-app>

工程下载http://dl.iteye.com/topics/download/22fe9c49-de7a-3a9c-bb77-863ef0983b4d

p.s.我粘贴的源码有更改,更改在有注释的地方的代码。工程未改。

猜你喜欢

转载自zjh776.iteye.com/blog/1697511