Struts2UI label

Sturts2 also gives us UI tags to simplify our development… The TAB that displays the page is…..

However, Struts2 is a server-side framework, so tags that use a page need to be parsed on the server and then parsed by the browser before being displayed on the page. ** Therefore, its performance is not good enough for HTML tags… HTML is directly parsed by the browser

One more thing: when you write a web page, you definitely need to use div+ CSS layout. Using Struts2UI tags also can’t dry…. Therefore, use Struts2UI tags only when necessary

Simply use Struts2UI tags


<%- We found that the Struts2UI tag works almost as well as HTML -%>

<s:form id="form1" name="form1" method="POST" action="#">


    <%- input box data,lable is equivalent to the data we used to write outside -%>
    <s:textfield label="Username" name="user.username"/>

    <%-- Password box --%>
    <s:password label="Password" name="user.password"/>

    <%-- Submit button --%>
    <s:submit value="Submit"/>

</s:form>

Copy the code

When we look at the source file, we find that the Struts2UI TAB automatically adds a lot of tags for us

Set the theme

As mentioned above, Struts2UI tags automatically add a lot of tags for us, so sometimes we don’t want to, or change the style, what to do??

The Struts2UI tag also gives us features like “themes”…

When we set the theme property to simple in the form… We found that all the tags that Struts2 had automatically added for us were gone.

If we wanted to use Simple as the theme for the entire project, we could do it in the configuration file!


  <constant name="struts.ui.theme" value="simple"/>

Copy the code

The echo data

We are also familiar with data echo, having already used it with EL expressions…. Why is the data echo in the Struts2 tag explained here? Struts2 also supports data echo, and to use data echo you must use Struts2 tags…

Write the data output in the normal way

Struts Struts Struts Struts Struts Struts Struts Struts Struts Struts Struts Struts

  • Put the data into the request domain for storage, jump to the corresponding JSP page…
    public String login(a) {
        // Store the data in the domain
        Map<String, Object> request = ActionContext.getContext().getContextMap();

        request.put("username"."zhongfucheng");
        request.put("password"."123");

        return SUCCESS;
    }
Copy the code
  • JSP pages use Struts2 tags and set the value attribute to perform data echo

<s:form id="form1" name="form1" method="POST" action="" >


    <%- input box data,lable is equivalent to the data we used to write outside -%>
    <s:textfield label="Username" name="user.username" value="%{#request.username}"/>

    <%-- Password box --%>
    <s:password label="Password" name="user.password"/>

    <%-- Submit button --%>
    <s:submit value="Submit"/>

</s:form>
Copy the code
  • Effect:


Struts2 data is displayed

Actually, the above code already implements data echo, but Struts2 provides a more subtle approach

In other words, Struts2 can implement data echo directly in the name…. Here’s what to do:

  • The data we return is stored in the root element of the CompoundRoot.

Therefore, we put the data we want to echo under CompoundRoot

  • The data placed directly on the value stack is the root element data

        // Get the stack object
        ValueStack valueStack = ActionContext.getContext().getValueStack();
        valueStack.set("username"."zhongfucheng");
Copy the code
  • You can get it directly in JSP

  <s:textfield label="Username" name="username"/>
Copy the code
  • Effect:


Struts2 tag: checkboxList

Reference from the blog: http://www.blogjava.net/koradji/articles/307399.html

Grammar:

<s:checkboxlist name="" list="" listKey="" listValue="" value="" />

  • Name defines the name of the label used to receive the selected check box on the screen, so it should be consistent with the properties defined in the Action, and most of the array;
  • List defines the collection variable used to print checkboxes to the screen. Typically, a list or Map property is defined in the Action;
  • ListKey If the Action is defined as a List, it will often define a Bean in the List, which has only two properties, one of which (such as ID) is set here;
    • If a Map is defined in the Action, the key of the Map is set here;
  • ListValue If you define a List in an Action, you will often define a Bean in the List with only two properties, and the other one (such as name) will be set there;
    • If the Action defines a Map, the value of the Map is set here;Copy the code
  • The value is used toThe selected check box on the display screenIf the screen has an input check, if there is an error will return to the original screen and display the error message, this time you need to use it.
    • You can set it to be the same as name.

Example:

Query all roles, get all ids of users and roles. Encapsulate it in an array.


    public String editUI(a) {

        // Query all the roles and bring them to the JSP page to display
        ActionContext.getContext().getContextMap().put("roleList", roleServiceImpl.findObjects());

        // We need to find the User that corresponds to the id
        if(user ! =null&&user.getId() ! =null  ) {
            // The User has a getter to read the corresponding information!
            user = userServiceImpl.findObjectById(user.getId());

            // Get the owning UserRole from the user id
            List<UserRole> roles = userServiceImpl.findRoleById(user.getId());
            // Fill the array with the user's role iD, which is displayed in the JSP page
            int i=0;
            userRoleIds =  new String[roles.size()];
            for(UserRole role : roles) { userRoleIds[i++] = role.getUserRoleId().getRole().getRoleId(); }}return "editUI";
    }
Copy the code

The name value is an array and can be used for automatic echo. This is also how Action gets the character ID


  <s:checkboxlist list="#roleList" name="userRoleIds" listKey="roleId" listValue="name"></s:checkboxlist>
Copy the code

Resource internationalization

We covered resource internationalization when we studied JSTL tags, but I didn’t write a blog post on how to do…. in the JSP chapter On the one hand feel JSP resource internationalization good trouble, on the other hand is to feel the place with very few….. So I didn’t get to know it…

Today, I learned about the internationalization of Struts2 resources. Go back to the steps for JSP resource internationalization and compare the Struts resource internationalization

Servlet and Struts resource internationalization differences

Write resource internationalization steps in Servlet:

  • Write resource file

    • Base name.properties [default]
    • Basic name _ Language abbreviation _ Country abbreviation. Properties
  • Reading configuration Files

    • JSTL provides a library of formatted and internationalized tags.

Write resource internationalization steps in Struts2:

  • Write resource file
    • Base name.properties [default]
    • Basic name _ Language abbreviation _ Country abbreviation. Properties
  • Read the configuration file before using it
    • ** Struts tag gets resource file content **

The difference between:

Struts2 makes it easier to load resource files through constant configuration! Struts tags are provided for easier use


Resource internationalization Demo

Let’s use Struts to write a resource internationalization Demo….

Write resource file

It is worth noting that if you write the properties resource file under IDEA, it will not help us to encode it by default if Chinese is involved. It’s not like Eclipse… Therefore, we need to manually set…… The setup tutorial is in my IDEA blog… If this problem occurs, go to the IDEA blog to find.

Load resource files using constants in Struts

We use the Contant node directly in Struts to load the configuration files we wrote ourselves. Note that the value of value is the full name of the file (that is, write the package name if there is one)…. And, no suffixes needed…

 <constant name="struts.custom.i18n.resources" value="msg"></constant>
Copy the code


Struts tags use internationalization

The value of name is the information configured in the configuration file, just write!


<head>
    <title><s:text name="title"></s:text></title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
</head>

<body>
<form method="post" action="${pageContext.request.contextPath }/user_login.action">
    <s:text name="username"></s:text> <input name="admin.userName"><br/>
    <s:text name="psd"></s:text><input type="text" name="admin.pwd"><br/>
    <input type="submit" value="Login"><br/>
</form>
</body>
Copy the code

test

Note that the title has been replaced with Chinese!!

If the article has the wrong place welcome to correct, everybody exchanges with each other. Students who are used to reading technical articles on wechat and want to get more Java resources can follow the wechat public account :Java3y