Objective
Build a Struts2 Web project from scratch, step-by-step.
Tools: Eclipse
Building process
First, create a dynamic Web project with the following structure:
We then add the jar packages needed for the project, put them under the Lib directory below the Web-inf, and add them to the project:
Jar Package: http://download.csdn.net/detail/zjq_1314520/9802042
Here we have all the basic jar packages we need, and we've chosen some of our simplest projects.
Then we'll configure the Web. xml
Here we configure a permission filter, filter all the paths, add the following code:
<?xml version= "1.0" encoding= "UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_ 0.xsd " id=" webapp_id " version=" 3.0 "> <display-name>HelloStruts2</display-name> <filter> <filter-name>Hellostruts</filter-name> <filter-class>Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>Hellostruts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></Web-app>
After the Web. XML configuration is complete, let's do some struts configuration.
Create the Struts.xml in the SRC directory, adding the following code:
<?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></struts>
Under the SRC directory, create a Struts2 Action (hellostrutsaction in) that inherits the Actionsupport base class.
Add the following code to the action:
package com.struts.trio;import com.opensymphony.xwork2.ActionSupport;publicclass HelloStrutsAction extends ActionSupport { @Override publicexecutethrows Exception { // TODO Auto-generated method stub return"index"; }}
In the above code we override the method used in the base class to handle the user request execute()
.
The return value of the above method is index
, then how to identify and return to the corresponding interface?
So we need to define the mapping between the logical view and the physical resource.
struts.xml
Add the following code in:
<struts> <!--Define the connection between a logical view and a physical view-- < package name="Trio" extends= "struts-default"> <action name="Index" class=" Com.struts.trio.HelloStrutsAction "> <!--to map index to physical address -- <result name="index">/index.jsp</result> </Action> </Package > <!--end --</struts>
This will return WebContent
the file below the directory index.jsp
.
We create this index.jsp
file and add the following code:
<%@ 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><head><meta http-equiv="Content-type" Content="text/html; Charset=utf-8 "><title>Insert Title here</title></head><body><H1>Hello struts2 Login success!</H1></body></html>
Here, a Struts2 project is finished, put on the server run, if the final result is as follows, you also created a success!
The above is mainly a Struts2
general model, the Action
usage of this is shown below:
Data interaction
You will now use the structure you just built to interact with the data.
Let's design an application scenario and define the requirements as follows:
- This is a user login page (user name, password)
- Login successful, jump to
success.jsp
- Login failed, jump to
failure.jsp
We will complete this requirement on the basis above:
First we will index.jsp
change to a file containing the login form:
Introduction Struts2
of Tag libraries:
<!-- 引入struts的标签库--><%@ taglib uri="/struts-tags" prefix="s"%>
The final code is as follows:
<%@ page language="java" contenttype="text/html; Charset=utf-8 "pageencoding="UTF-8"%> <!--introducing Struts's tag library--<%@ taglib uri="/struts-tags" prefix="s"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" ><html><head><meta http-equiv="Content-type" Content="text/html; Charset=utf-8 "><title>Login</title></head><body> <s:form Action="index"> <s:textfield name="username" key="user name"></S:textfield> <s:textfield name="password" key="secret name"></S:textfield> <s:submit Key="Login"></s:submit> </s:form></body></html>
Note 1: More about Struts2
the label can be consulted: Struts2 label
Note 2: The submission Address (index) in the form corresponds to the struts.xml
configuration here:
Description, click on the button and the data will be uploaded to com.struts.trio.HelloStrutsAction
this Action
.
Next we will accept the data from the front desk:
When the user name is: admin, password is: 123456 time login success, and vice versa failed.
Then, Action
the code in is modified to read as follows:
PackageCom.struts.trio;ImportCom.opensymphony.xwork2.ActionSupport; Public class hellostrutsaction extends actionsupport { //define the username and password of request parameters //Same as the name attribute in the form in the foreground JSP PrivateString username;PrivateString password; PublicStringGetUserName() {returnUsername } Public void Setusername(String username) { This. Username = Username; } PublicStringGetPassword() {returnPassword } Public void SetPassword(String password) { This. Password = password; }@Override PublicStringExecute()throwsException {//TODO auto-generated method stubString username= This. GetUserName (); String userpassword= This. GetPassword ();if(Username.equals ("Admin") &&userpassword.equals ("123456") {Actioncontext.getcontext (). GetSession (). Put ("User", userName);//user name stored in session for return interface display return "Success"; }return "Failure"; }}
Because Action
the return value has changed, the struts.xml
corresponding mapping in is changed to:
<struts> <!--Define the connection between a logical view and a physical view-- < package name="Trio" extends= "struts-default"> <action name="Index" class=" Com.struts.trio.HelloStrutsAction "> <result name="Success">/jsp/success.jsp</result> <result name="Failure">/jsp/failure.jsp</result> </Action> </Package > <!--end --</struts>
Next we create the corresponding JSP files for the two return interface:
Direct display on login failure interface: Login failed
Login Success Screen display: XXX (username) Login Successful
The corresponding is failure.jsp
not too much to explain, then success,jsp
how to output the file background stored session
in the user name it?
ActionContext.getContext().getSession() .put("user", userName); //用户名存储在 session 用于返回界面显示
This is the code that stores the user name, the stored key is "user"
We use the EL expression in the JSP to get the value in the session, some of the code is as follows:
</head><body><h1>${session.user} 登录成功</h1></body>
OK, this simple example is finished, check if your project can run correctly!
Project Testing
Run project, enter user name password
Click Sign In
Show login success, now, the instance is complete!
To create a STRUTS2 Web project