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.
<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>
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&userpass= Userpass
-
<result name= "Success" >/page/demo.jsp</result>
</action>
</package>
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>
<body>
${message}
</body>
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" >
<struts>
<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>
</action>
</package>
<!--ADD packages here--
</struts>
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)