[Struts2 Study Notes] section 3 six steps required to create struts 2 HelloWorld, struts2helloworld

Source: Internet
Author: User

[Struts2 Study Notes] section 3 six steps required to create struts 2 HelloWorld, struts2helloworld

Address: http://blog.csdn.net/sushengmiyan/article/details/40349201

Official documents: http://struts.apache.org/release/2.3.x/docs/hello-world-using-struts-2.html

Author: sushengmiyan

Certificate ------------------------------------------------------------------------------------------------------------------------------------

When you click a hyperlink in the Struts 2 web application or submit a form data, the input data is not transferred to another page on the server, but to a Java class you provide. these classes are called Actions. when an Action is fired, a resource is selected to display the response. generally, a resource is a webpage, a PDF file, an Excel tab, or a Java applet window.

Suppose you want to create a simple "Hello World" example to show the welcome information. after setting up the struts work environment (refer to how to create a struts 2 web application) and creating a "Hello World" example, you need to do the following four things:

  1. Create a java class (model) that stores welcome information)
  2. Create a page for displaying information (view)
  3. Create an Action class to control the relationship between users, models, and views (controller)
  4. Create a mapping (struts. xml) to combine Action classes and views.
This article assumes that you have completed how to create a Struts 2 Web application experience and have a basic struts workspace. the source code for this helloworld experience can be downloaded from the Struts 2 GitHub repository https://github.com/apache/struts-examples. the example project uses maven to manage dependencies and structures. war file.


Step 1-create a model class MessageStore. java

In our first example, we create a new package com. susheng. struts2Maven. bean and create a MessageStore class in it. The content is as follows:

package com.susheng.struts2Maven.bean;public class MessageStore {    private String message;        public MessageStore() {                 setMessage("Hello Struts User");    }     public String getMessage() {         return message;    }     public void setMessage(String message) {         this.message = message;    }}

Step 2-create Action class HelloWorldAction. java

Create a package com. susheng. struts2Maven. action. Under this package, create a class HelloWorldAction. The content is as follows:

package com.susheng.struts2Maven.action;import com.opensymphony.xwork2.ActionSupport;import com.susheng.struts2Maven.bean.MessageStore;public class HelloWorldAction extends ActionSupport{   private static final long serialVersionUID = 1L;       private MessageStore messageStore;         public String execute() throws Exception {                 messageStore = new MessageStore() ;        return SUCCESS;    }     public MessageStore getMessageStore() {        return messageStore;    }     public void setMessageStore(MessageStore messageStore) {        this.messageStore = messageStore;    }}



Step 3-create View HelloWorld. jsp

The content is as follows:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


Step 4-add Struts configuration to struts. xml


The complete content is as follows:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>     <constant name="struts.devMode" value="true" />     <package name="basicstruts2" extends="struts-default">         <action name="index">            <result>/index.jsp</result>        </action>                <action name="hello" class="com.susheng.struts2Maven.action.HelloWorldAction" method="execute">          <result name="success">/HelloWorld.jsp</result>        </action>    </package> </struts>

Step 5-create a URL Action

Modify index. jsp to the following:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

Step 6-construct the WAR file and run the program. The war package I constructed is basic_struts.war, so my access link is http: // localhost: 8080/basic_struts/index. action. Click the helloworld link.



The second example is completed.


How struts2 works:



Struts2 HelloWorld

Your project is not started.

Struts2 HelloWorld

Step 1: install Tomcat 6.jdk1.6. (This is my environment and should not be necessary)
Step 2: index.html is used to jump to helloworld. action. The Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<Html>
<Head>
<META HTTP-EQUIV = "Refresh" CONTENT = "0; URL = example/HelloWorld. action">
</Head>

<Body>
<P> Loading... </p>
</Body>
</Html>
Step 3: HelloWorld. jsp under the example directory. The Code is as follows:
<% @ Page contentType = "text/html; charset = UTF-8" %>
<% @ Taglib prefix = "s" uri = "/struts-tags" %>
<Html>
<Head>
<Title> <s: text name = "HelloWorld. message"/> </title>
</Head>

<Body>
<H2> <s: property value = "message"/>
<H3> ages <Ul>
<Li>
<S: url id = "url" action = "HelloWorld">
<S: param name = "request_locale"> en </s: param>
</S: url>
<S: a href = "% {url}"> English </s: a>
</Li>
<Li>
<S: url id = "url" action = "HelloWorld">
<S: param name = "request_locale"> es </s: param>
</S: url>
<S: a href = "% {url}"> Espanol </s: a>
</Li>
</Ul>

</Body>
</Html>
Step 4: example package... the remaining full text>

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.