Use of struts2 Convention plug-in (1)

Source: Internet
Author: User

I just checked the official document (convention-plugin.html) and learned the Convention plug-in of struts2. The article serves as only one note. I suggest you go to the official document for clarity and comprehensiveness.

You need to add these packages in the project

 

Convention first looks for the package. If the package contains the word action, the package is used as the root path, that is, namespace = "/"

Then find the class. If the class name of a class is the end of action or inherits the actionsupport class, the class will be used as the action class, and the corresponding URL is

For example:

Com. example. Actions. mainaction->/main

Com. example. Actions. Products. display->/products/display

Com. example. Struts. Company. Details. showcompanydetailsaction->/Company/details/show-company-Details

 

JSP files to be placed under the/WEB-INF/content/folder

/WEB-INF/content/Main. jsp

/WEB-INF/content/display. jsp

/WEB-INF/content/Company/details/show-company-details.jsp

 

The following is an example:

Struts. xml:

<? 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> <constant name = "struts. i18n. encoding "value =" UTF-8 "/> <! -- When the struts configuration file is modified, whether the system automatically reloads the file. The default value is false (used in the production environment). It is best to open the file during development --> <constant name = "struts. configuration. XML. reload "value =" true "/> <! -- Used in development mode to print more detailed error information --> <constant name = "struts. devmode" value = "true"/> </struts>

Web. xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    <display-name>Struts Blank</display-name>    <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>    <welcome-file-list>        <welcome-file>index.html</welcome-file>    </welcome-file-list></web-app>

The package structure is as follows:

Helloworld. Java

package com.hyy.action;import com.opensymphony.xwork2.ActionSupport;public class HelloWorld extends ActionSupport{    private String message;    public String execute() {        message = "hyy";        return SUCCESS;    }    public String getMessage() {        return message;    }}

/WEB-INF/content/hello-world.jsp

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> 

Enter http: // localhost: 8080/project name/Hello-world in the address bar.

The action helloworld. Java, the returned view is the hello-world.jsp

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.