eclipse learning (Chapter 2: getting to know ssh) - 15 Struts 2 theme and template (just understand)

termdescribe
tag (label)A small piece of code executed from within JSP, FreeMarker, or Velocity.
templateSome code, usually written on FreeMarker, can be rendered by some tags (HTML tags).
themeA collection of templates encapsulated together to provide common functionality.

When you use struts 2 Tags in Web pages (such as < s: submit... >, < s: textfield... >, etc.), the struts 2 Framework generates HTML code with pre configured styles and layouts. Struts 2 has three built-in themes:

Themedescribe
simple themeThere is no minimum theme of "bells and whistles". For example, textfield tags render HTML tags without tags, validation, error reporting, or any other format or functionality.
xhtml themeThis is the default theme used by Struts 2. It provides all the foundations of simple theme and adds several functions, such as standard two list layout of HTML, tag of each HTML, verification and error reporting.
css_xhtml themeThis topic provides all the basics of simple theme and adds several functions, such as standard two column layout based on CSS and the use of HTML Struts tags
HTML, struts, tag of each tag, placement according to CSS style sheet, etc.

preface

This article refers to the notes made after practice from a website
https://www.w3cschool.cn/struts_2/struts_themes_templates.html

How to set a theme

Set properties directly

<s:textfield name="name"  theme="xhtml"></s:textfield>

We can set an index JSP and a simple action (ps: any definition of action is OK, and the action defined in struts.xml in this test is paly)
index.jsp contents are as follows

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<s:form action="play" method="post">
		name:<s:textfield name="name" theme="xhtml" ></s:textfield>
		<s:submit value="Submit"/>
	</s:form>
	
</body>
</html>

The page access results are as follows

The source code of the web page is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form id="play" name="play" action="/ssh_learn_topicAndModule/play.action" method="post">
<table class="wwFormTable">
		name:<tr>
    <td class="tdLabel"></td>
    <td
><input type="text" name="name" value="" id="play_name"/></td>
</tr>


		<tr>
    <td colspan="2"><div align="right"><input type="submit" value="Submit" id="play_0"/>
</div></td>
</tr>


	</table></form>



	
</body>
</html>

Through comparison, it is known that:

<s:textfield name="name" theme="xhtml" ></s:textfield>

In fact, the final display in the front page is like this. The id here is generated according to your action.

<tr>
    <td class="tdLabel"></td>
    <td
><input type="text" name="name" value="" id="play_name"/></td>
</tr>

essence

How to realize from s:xxx to a new code block? This depends on the jar package we pulled -- struts 2-core x. Y.z.jar file. We can try to decompress it and see what's inside.
For a given topic, each struts tag has an associated template, such as s: textfield - > text FTL and s: password - > password FTL et al. These template files are compressed in struts 2-core x. In the y.z.jar file, they keep a predefined HTML layout for each tag. Therefore, the struts 2 framework uses Sturts tags and related templates to generate the final HTML tag code.
After decompression, you can see a template folder inside

Struts 2 tags + Associated template file = Final HTML markup code
struts2 label+Associated template file=Final HTML Tag code

The default template style is: extension ftl

One last word

In fact, I think it's better for you to set this style than to set the label style directly. This is ok, and basically different styles are required in different situations, so I don't think it's of great use

Keywords: Eclipse struts2

Added by mirana on Wed, 09 Feb 2022 08:45:12 +0200