Differences between JSF and Struts

 Struts and JSF / Tapestry belong to the presentation layer framework, these two belong to different nature of the framework, which is an event-driven component model, but simply Struts MVC model frameworks, foreigners always fixing authorities merely said event driven better than the MVC model frameworks, how could one say, for detailed analysis below we compare in the end is how is it?

  First event is a page from the client (browser) by an event triggered by user actions, Struts Action using the event to accept the browser form submission, used here Command mode, each subclass inherits Action must implement a method execute .

  In the struts, it is actually a form corresponding to Form an Action class (or the DispatchAction), In other words: In Struts is actually a form corresponding to only one event, this event is called struts application event, application event and component event compared event is a coarse - degrees.

  struts important ActionForm form object is an object that represents an application, this object contains at least a few fields that are Jsp pages in the form of input fields, because a form corresponding to an event, so when we need when the event granularity down to form these fields, that is, when a field corresponds to an event, simply use Struts less likely, of course, through a combination of JavaScript can also turn achievable.

  And this situation can be easily achieved using JSF,

<h:inputText id="userId" value="#{login.userId}">
  <f:valueChangeListener type="logindemo.UserLoginChanged" />
</h:inputText>

  # {Login.userId} represents the results obtained from the login name JavaBean getUserId, this function can be realized using struts, name = "login" property = "userId"

  The key is the second line, here represented value changes and determine if userId after submission will trigger processValueChanged (...) method calls the class UserLoginChanged.

  JSF components can provide both events:. Value Changed Action and the usefulness of the former we've seen in the last section, which is equivalent to the struts form submission Action mechanism, its JSF worded as follows:

<h:commandButton id="login" commandName="login">
  <f:actionListener type=”logindemo.LoginActionListener” />
</h:commandButton>

  As can be seen from the code, the two events are accomplished by the observer pattern Listerner assembly affixed to the particular field, and the original Struts such events is a triggering mechanism Submit form submission. If the former is the language of the (Swing-like programming language programming practices); the latter is part of the WEB, because it comes from Html form, if you start from the beginning Perl / PHP, but easy to accept Struts this style.

basic configuration

  Struts and JSF is a framework, JSF JSF package must require two core packages, JSTL package (tag library). In addition, JSF will use some Apache commons project packages, these packages as long as the deployment of Apache on your server either.

  JSF package Download: http://java.sun.com/j2ee/javaserverfaces/download.html select Reference Implementation.

  JSTL package download http://jakarta.apache.org/site/downloads/downloads_taglibs-standard.cgi

  Therefore, the composition of watching from the JSF driver package, which is also open source genes account for a large proportion, is a SUN JSF industrial partners a hybrid between the standard and open source.

  The above-described two address to download the jar is JSF combined all the necessary drive package. Struts and driver package, these packages must be located in the drive WEB-INF / lib Web project, and is Struts must also have the same configuration as in web.xml:

<web-app>
  <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>

  Here and Struts web.xml configuration of much alike, so alike.

  As the Struts struts-config.xml, like, JSF have similar faces-config.xml configuration file:


<faces-config>
  <navigation-rule>
    <from-view-id>/index.jsp</from-view-id>
    <navigation-case>
      <from-outcome>login</from-outcome>
      <to-view-id>/welcome.jsp</to-view-id>
    </navigation-case>
  </navigation-rule>

  <managed-bean>
    <managed-bean-name>user</managed-bean-name>
    <managed-bean-class>com.corejsf.UserBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>
</faces-config>

 

  There in the Struts-config.xml ActionForm Action process and the relationship between Jsp in the faces-config.xml, there are also such a process, we specifically explain Navigation:

  There is an event in index.jsp:

<h:commandButton label="Login" action="login" />

  Value of the action must match the form-outcome value of the Navigation configuration, said: if there is a login event in index.jsp, then the next page after a trigger event will be welcome.jsp

  JSF has a separate incident and page navigation process arrangements, thinking this to be very clear than the struts.

  Struts managed-bean similar to the ActionForm, as may be defined in the scope of the ActionForm struts-config.xml as in, here defined as the scope of the managed-bean session.

  But if you only think that the JSF managed-bean In this function would be wrong, JSF into a new mode Ioc / dependency injection techniques.

Ioc mode

  For Userbean such a managed-bean, which code is as follows:

public class UserBean {
  private String name;
  private String password;

  // PROPERTY: name
  public String getName() { return name; }
  public void setName(String newValue) { name = newValue; }

  // PROPERTY: password
  public String getPassword() { return password; }
  public void setPassword(String newValue) { password = newValue; }
}

 

<managed-bean>
  <managed-bean-name>user</managed-bean-name>
  <managed-bean-class>com.corejsf.UserBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>

  <managed-property>
    <property-name>name</property-name>
    <value>me</value>
  </managed-property>

  <managed-property>
    <property-name>password</property-name>
    <value>secret</value>
  </managed-property>
</managed-bean>

  faces-config.xml这段配置其实是将"me"赋值给name,将secret赋值给password,这是采取Ioc模式中的Setter注射方式

Backing Beans

  对于一个web form,我们可以使用一个bean包含其涉及的所有组件,这个bean就称为Backing Bean, Backing Bean的优点是:一个单个类可以封装相关一系列功能的数据和逻辑。

  说白了,就是一个Javabean里包含其他Javabean,互相调用,属于Facade模式或Adapter模式。


  对于一个Backing Beans来说, 其中包含了几个managed-bean,managed-bean一定是有scope的,那么这其中的几个managed-beans如何配置它们的scope呢?

<managed-bean>
  ...
  <managed-property>
    <property-name>visit</property-name>
    <value>#{sessionScope.visit}</value>
  </managed-property>

  这里配置了一个Backing Beans中有一个setVisit方法,将这个visit赋值为session中的visit,这样以后在程序中我们只管访问visit对象,从中获取我们希望的数据(如用户登陆注册信息),而visit是保存在session还是application或request只需要配置既可。

UI界面

  JSF和Struts一样,除了JavaBeans类之外,还有页面表现元素,都是是使用标签完成的,Struts也提供了struts-faces.tld标签库向JSF过渡。

  使用Struts标签库编程复杂页面时,一个最大问题是会大量使用logic标签,这个logic如同if语句,一旦写起来,搞的JSP页面象俄罗斯方块一样,但是使用JSF标签就简洁优美:

<jia:navigatorItem name="inbox" label="InBox"
  icon="/images/inbox.gif"
  action="inbox"
  disabled="#{!authenticationBean.inboxAuthorized}"/>

  如果authenticationBean中inboxAuthorized返回是假,那么这一行标签就不用显示,多干净利索!

转载自  https://www.jdon.com/idea/jsf-struts.htm

发布了274 篇原创文章 · 获赞 144 · 访问量 7937

Guess you like

Origin blog.csdn.net/qq_44868502/article/details/103950025
jsf