Learning to get started with struts (i)

Source: Internet
Author: User

Just beginning to learn the framework when the feeling is very simple, are used to the relevant framework of Java EE, I want to study the source code, but learned a long time after the no clue, so it is hard to learn struts after all, the framework does not have to write Java EE to be simple, Now let's take a step-by-stage study of struts,

The first step to learn is to add the jar package can go to Http://pan.baidu.com/s/1c0fHb8W here to download, including the jar package and configuration file to import the jar package into the project's Lib directory, configuration file import src can

The second part is to modify the configuration file first add the following code in Web. XML here, there's not much to explain.



Then configure the dependencies in the Struts.xml to

Let's take a brief look at the configuration file.


In general, Java is the same as the package is the organizational unit, but the package here is not in Java Package options include package name

namespace is the path to access, extends is the core package that inherits the core package before it can use the relevant core functionality


<package name= "PackageName" namespace= "/test" extends= "Struts-default" >


The action is the exact path to access the action name, which is the absolute path to the class to access, and method is the way to access the class


<action name= "Demo" class= "Com.test.Demo" method= "execute" >


Name means that the Execute method returns a string if the success is forwarded to the path specified by result

What if it's a redirect? As long as you add a parameter type= "redirect", you know that redirection cannot be passed with parameters then we have to consider the browser passed the/page/demo.jsp?username=username&amp;userpass= Userpass


<result name= "Success" >/page/demo.jsp</result>


Now let's do a Helloword program.

1 Create Java class, here we create helloword.class under Com.test

Package com.test;

public class Helloword {

Private String message;

Public String GetMessage () {
return message;

public void Setmessage (String message) {
this.message = message;

Public String execute () {
This.message = "This is the hair of the video can also be done";
Return "Success";

2 Create/web-inf/page/demo.jsp simple display data to

<%@ 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" >
<meta http-equiv= "Content-type" content= "text/html; Charset=iso-8859-1 ">
<title>insert title here</title>

Modifying a configuration file

<?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" >


<package name= "Itcast" namespace= "/test" extends= "Struts-default" >
<action name= "Helloword" class= "Com.test.HelloWord" method= "execute" >
<result name= "Success" >/WEB-INF/page/demo.jsp</result>

<!--ADD packages here--


Of course, the release of the project or something here is not BB, my browser input http://localhost/Struts/test/helloword.action

Learning to get started with struts (i)

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.