-->

Why do we use struts tags and not the old HTML tag

2020-07-23 08:32发布

问题:

I am new to struts , and wanna know why do we refer struts tags over previous html tags when they both seem to be doing the same thing , what is the advantage of using struts tags over html tags . like

<s:form action="myAction">
    <s:textfield name="username"></s:textfield>
    <s:textfield name="password"></s:textfield>
    <s:submit name="submit" value="submit"></s:submit>
</s:form>

<form action="myAction" method="post">
    <input type="text" name="username"/>
    <input type="text" name="password"/>
    <input type="submit" name="submit"/>
</form>

I have googled it , but have not got any satisfying answer , help is appreciated in advance

I know it adds some extra things like labels or something , that is fine with me , but what i wanna know if it does anything with the performance or anything specifically to the request object , anything other just avoiding the extra typing for the programmer .

回答1:

There are different types of Struts 2 tags.

Form tags in particular are beneficial, but their functionality depends entire on the Struts 2 theme being used. The "simple" theme provides almost no benefit beyond standard HTML tags. The default "xhtml" theme emits form table rows and some styling. Custom themes implement whatever functionality you desire. Regardless of the theme the underlying JSP source never needs to change.

The primary advantages being:

  • Validation error messages
  • Form presentation/styling
  • Framework integration (e.g., value automatically filled from action)

There are also a set of Dojo (very deprecated) and jQuery tags that wrap up a fair amount of JavaScript functionality in custom tags, which makes simple dynamic functionality very simple.

There are also non-view tags, like <s:iterator> etc. Some of their functionality is a duplicate of JSTL (<c:forEach> in the case of the iterator tag). There aren't a huge number of benefits to using the S2 tags in this case when functionality overlaps.

Some tags have no JSTL analog, like <s:merge> or <s:sort>. , Whether or not that's functionality suitable for the view layer is a different discussion. Some provide integration into other aspects of Struts 2, like I18N (e.g., <s:text>). Some understand about Struts 2 configuration (e.g., <s:url>).

The purpose of any custom tag is to simplify view layer programming (e.g., JSP). S2 provides a lot of tags to help with that. Some are simple and generic (non-UI tags), while the UI tags are implemented using FreeMarker and are heavily customizable.



回答2:

In this particular example, not a lot, but the benefit is it gets replaced with HTML at parse time, and what it gets replaced with is customizable. You can define a standard form template, and have <s:form> replaced with that.

In other words, it's (theoretically) to standardize site behavior and minimize typing.

Update: Steven pointed out an even more valuable reason, that Struts can intelligently populate and update an <s:form> field, such as leaving a form filled in after a submission error.



回答3:

Struts purely uses JAVA! Hence even if the programmer doesn't have knowledge about HTML/JAVASCRIPT he can still work out his way using the built in tags of struts. Struts also provides the inbuilt facility of validation just like Javascript.



标签: java struts2