Seam practices (2): automatically generated code

Source: Internet
Author: User
A lot of code is generated after the project is created. It is basically a configuration file. During seam development, you do not need to modify the configuration file all day long. Write pages. xml or faces-config.xml at most. The structure of the folder generated by seam is as follows:
. The build folder stores ant compiled items.
Resources folder. XXXX-ds.xml. Is used to store the data source configuration file.
SRC:
SRC has two folders: Action and modal. It stores Page Actions and Domain Models.
There is a META-INF folder in modal where the JPA configuration file persistence. XML is stored.
There are several notable files in the modal folder.
For example, messages_en.properties and security. DRL. Messages_en.properties stores system messages.
If you want the system message of seam to display Chinese characters, you need to translate this file. Security. DRL defines security rules.
In the action folder. There is a package: ORG/domain/seamtest/session. Authenticator. Java is automatically generated. Used for login verification. Here is a brief introduction, first look at the Code:

@ Name ("authenticator ")
Public class authenticator
{
@ Logger log;

@ In identity;

Public Boolean authenticate ()
{
Log.info ("authenticating #0", identity. GetUserName ());
// Write your authentication logic here,
// Return true if the authentication was
// Successful, false otherwise
Identity. addrole ("admin ");
Return true;
}
}

@ Name is used to define the seam component. The defined seam component can be used for Bidirectional injection and other page operations. If you do not have this comment. It indicates that this is not a seam component, so it also serves to identify the seam component. Here, the seam component is named "authenticator". To call the Authenticate Method of this component on the page, write as follows: # {authenticator. Authenticate}
@ Logger: used to inject log Components
@ In is used for Bidirectional injection. During injection, seam looks for components in the current container that match the variable name. Of course. You can also inject components with different variable names. You must specify the component name, for example, @ in ("ident "). This means to find the component named "ident" in the container and inject it into the class. (The scope of seam is complex. I will not introduce it here)
The next step is the authenticate method. The most important part of this method is the next two sentences. Log.info (...) means to save the log information, and the last meaning is verified. If the authenticated user name and password do not match, return false; indicates that login is denied. The more complex is identity. addrole. This method adds a role to the current user. If this method is used, false is returned. The added roles are not saved.
So. How can I let seam know how to call this method for verification when a user logs in? There is such a configuration in/webcontent/WEB-INF/components. xml:

<Security: identity authenticate-method = "# {authenticator. Authenticate }"
Security-rules = "# {securityrules }"
Remember-Me = "true"/>

Authenticate-method indicates the authentication method. It is called as a component. Another security-rules attribute is a security rule. Where is the configuration? On the top. <Drools: Rule-base name = "securityrules">
<Drools: Rule-files> <value>/security. DRL </value> </drools: Rule-files>
</Drools: Rule-base>

This is the file we just mentioned.
Now that we have talked about components. XML, let's take a look at some other things in this file. <Core: init DEBUG = "true" JNDI-pattern = "@ jndipattern @"/>

This Code defines the JNDI search rule. @ Jndipattern @ is defined as the following configuration in/src/Modal/components. properties :#
# Fri dec 05 10:37:03 CST 2008
Jndipattern = // # {ejbname}/local
Embeddedejb = false

This configuration indicates that the JNDI search rule is the EJB component name/local. That is, the local EJB component is used. Instead of remote EJB components <core: Manager Concurrent-request-Timeout = "500"
Conversation-Timeout = "120000"
Conversation-ID-parameter = "CID"
Parent-conversation-ID-parameter = "PID"/>

Conversation-Timeout conversation (page stream) expiration time. It may be inappropriate to call conversation a page stream. You can call it according to your understanding. It is the process definition of a page flow. Seam defines a conversation scope.
Conversation-ID-parameter is used to define the reuqest parameter name of conversation. This is because the browser needs to return a parameter in the page stream that the user is currently in.
Parent-conversation-ID-parameter. Conversation allows you to define subpage streams. This attribute defines the parameter name for the browser to return the parent conversation <Persistence: managed-persistence-context name = "entitymanager"
Auto-create = "true"
Entity-Manager-factory = "# {seamtestentitymanagerfactory}"/>

This is the definition of the entitymanager component of JPA. @ In ("entitymanager") is automatically injected into the seam component.

<Event type = "org. JBoss. seam. Security. notloggedin">
<Action execute = "# {redirect. capturecurrentview}"/>
</Event>
<Event type = "org. JBoss. seam. Security. loginsuccessful">
<Action execute = "# {redirect. returntocapturedview}"/>
</Event>

This is for login. You must log on to a page. But the user has not logged on. This will go to a login page. The page to be transferred to the user after login. If you have such a requirement, add these two statements. However, seam is automatically generated.
Others: Chinese is not supported in the default generated face-config.xml. Can join <Application>
<View-handler> com. Sun. facelets. faceletviewhandler </View-handler>
<Locale-config>
<Default-locale> en </default-locale>
<Supported-locale> BG </supported-locale>
<Supported-locale> de </supported-locale>
<Supported-locale> en </supported-locale>
<Supported-locale> fr </supported-locale>
<Supported-locale> tr </supported-locale>
</Locale-config>
</Application>

Code: <supported-locale> zh_cn </supported-locale>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.