JAVAEE learning-struts2_01: introduction, setup, architecture, configuration, action class details and exercises: customer list, javaeestruts2_01

Source: Internet
Author: User

JAVAEE learning-struts2_01: introduction, setup, architecture, configuration, action class details and exercises: customer list, javaeestruts2_01

 

I. What is struts2?

1. Concept

  

2. Advantages and history of struts2

  

 

2. Build the struts2 framework

1. Import package

(Unzip) The struts2-blank.war will see

  

2. Write Action class

public class HelloAction {    public String hello(){                System.out.println("hello world!");                return "success";    }}
View Code

3. Write src/struts. xml (remember to add the dtd constraint)

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <package name="hello" namespace="/hello" extends="struts-default" >        <action name="HelloAction" class="cn.itheima.a_hello.HelloAction" method="hello" >            <result name="success" type="dispatcher" >/hello.jsp</result>        </action>    </package></struts>
View Code

4. Configure the struts2 core filter to web. xml

Filter-class cannot remember. You can press ctrl + shift + t to enter strutsPre to find the full class name.

<! -- Struts2 core filter --> <filter-name> struts2 </filter-name> <filter-class> org. apache. struts2.dispatcher. ng. filter. strutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>
View Code

5. Test

  

 

Iii. struts2 access process & struts2 Architecture

  1. struts2 Architecture

  

2. Aop programming (Aspect-Oriented)

  

 

Iv. configuration details

1. struts. xml configuration details

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <! -- I18n: International. Resolve post submission garbled --> <constant name = "struts. i18n. encoding" value = "UTF-8"> </constant> <! -- Specify the suffix http: // localhost: 8080/struts2_day01/hello/HelloAction for the reverse question action. do --> <constant name = "struts. action. extension "value =" action "> </constant> <! -- Specify whether struts2 runs in development mode. hot load master configuration. (No need to restart to take effect) 2. more error messages are provided to facilitate debugging during development --> <constant name = "struts. devMode "value =" true "> </constant> <! -- Package: encapsulate the Action configuration. you can configure many actions in the Package. name attribute: name of the package. start at will. the other package names cannot be repeated. namespace attribute: defines a namespace extends attribute in the action access path: inherits the abstract attribute of a specified package: whether the package is abstract or not; and the flag attribute. the package cannot run independently. specially inherited --> <package name = "hello" namespace = "/hello" extends = "struts-default"> <! -- Action element: configure the action class name attribute: determines the Resource name accessed by the Action. class Attribute: full class name method attribute of action: Specifies the method in the Action to be called to process the request --> <action name = "HelloAction" class = "cn. itheima. a_hello.HelloAction "method =" hello "> <! -- Result element: name attribute of result configuration: name of result processing. corresponds to the return value of the action method. type attribute: Specifies which result class is called to process the result. Forwarding is used by default. TAG body: Enter the relative path of the page --> <result name = "success" type = "dispatcher">/hello. jsp </result> </action> </package> <! -- Introduce other struts configuration files --> <include file = "cn/itheima/B _dynamic/struts. xml "> </include> <include file =" cn/itheima/c_default/struts. xml "> </include> </struts>
View Code

2. struts2 constant Configuration

2.1 struts2 default constant configuration location

    

2.2 modify the struts2 constant configuration (the mode is also the Loading Order)

Method 1: src/struts. xml (important)

<! -- I18n: International. Resolve post submission garbled --> <constant name = "struts. i18n. encoding" value = "UTF-8"> </constant>

Method 2: Create struts. properties under src

        struts.i18n.encoding=UTF8

Method 3: In the project's web. xml

          <context-param>              <param-name>struts.i18n.encoding</param-name>              <param-value>UTF-8</param-value>          </context-param>

Sequence:

      

2.3 constant Configuration

<! -- I18n: International. Resolve post submission garbled --> <constant name = "struts. i18n. encoding" value = "UTF-8"> </constant> <! -- Specify the suffix http: // localhost: 8080/struts2_day01/hello/HelloAction for the reverse question action. do --> <constant name = "struts. action. extension "value =" action "> </constant> <! -- Specify whether struts2 runs in development mode. hot load master configuration. (No need to restart to take effect) 2. more error messages are provided to facilitate debugging during development --> <constant name = "struts. devMode "value =" true "> </constant>
View Code

3. Advanced struts2 Configuration

3.1 dynamic method call (important)

<! -- Whether to enable a constant for dynamic method call is disabled by default. You need to enable --> <constant name = "struts. enable. DynamicMethodInvocation" value = "true"> </constant>
Method 1
<! -- Dynamic method call Method 2: Use the wildcard method {1} to retrieve the content with the first asterisk wildcard --> <action name = "Demo1Action _ *" class = "cn. itheima. B _dynamic.Demo1Action "method =" {1} "> <result name =" success ">/hello. jsp </result> </action>
Method 2

3.2struts2 default configuration (learn more)

<Package name = "default" namespace = "/default" extends = "struts-default"> <! -- If the action under the package is not found, Demo2Action is used as the default action to process the request --> <default-action-ref name = "Demo2Action"> </default-action-ref> <! -- Method property: execute --> <! -- Name attribute of result: success --> <! -- Type attribute of result: dispatcher forwarding --> <! -- Class attribute: com. opensymphony. xwork2.ActionSupport --> <action name = "Demo2Action"> <result>/hello. jsp </result> </action> </package>
View Code

 

5. action

Action Writing Method

 

// Method 1: Create a class. it can be POJO // POJO: do not inherit any parent class. no interface is required. // lower the code intrusion of the struts2 framework. public class Demo3Action {}
Method 1
// Method 2: Implement an interface Action. // The execute method is provided to provide the action method specification. // some strings are preset in the Action interface. you can use it when returning results. to facilitate public class Demo4Action implements Action {@ Override public String execute () throws Exception {return null ;}}
Method 2
// Method 3: inherit a class. actionSupport // helps us implement Validateable, ValidationAware, TextProvider, and LocaleProvider. // if you need to implement these interfaces, you do not need to implement them yourself. public class Demo5Action extends ActionSupport {}
Method 3

 

6. Exercise: customer list

Illustration:

  

Implementation:

Public class CustomerAction extends ActionSupport {private CustomerService cs = new CustomerServiceImpl (); public String list () throws Exception {// 1 accept parameter String cust_name = ServletActionContext. getRequest (). getParameter ("cust_name"); // 2 create an offline query object: DetachedCriteria dc = DetachedCriteria. forClass (Customer. class); // 3 determine the parameter Assembly condition if (StringUtils. isNotBlank (cust_name) {dc. add (Restrictions. like ("cust_name", "%" + cust_name + "%");} // 4 call the Service to pass the offline Object List <Customer> list = cs. getAll (dc); // 5 put the returned list in the request field. forward to list. jsp displays ServletActionContext. getRequest (). setAttribute ("list", list); return "list ";}}
Web Layer Code
Public List <Customer> getAll (DetachedCriteria dc) {Session session = HibernateUtils. getCurrentSession (); // open the Transaction tx = session. beginTransaction (); List <Customer> list = customerDao. getAll (dc); // closes the transaction tx. commit (); return list ;}
Service Layer Code
Public List <Customer> getAll (DetachedCriteria dc) {// 1 get session Session session = HibernateUtils. getCurrentSession (); // 2 associate the offline object with session Criteria c = dc. getExecutableCriteria (session); // 3. Execute the query and return c. list ();}
Dao Layer Code

 

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.