Objective
Recently there is time to summarize, the past knowledge of the course, although things are entry-level, the master must be dismissive, but for the first small white, still can provide a point of reference.
Struts2 actually encapsulates the servlet for us, simplifies the complex operation of JSP jumps, and provides easy-to-write tags to quickly develop the code for the view layer.
In the past, we used JSP and servlet collocation to achieve the presentation, the general process is:
1 JSP Trigger action
2 servlet accepts action and gives background class processing
3 Background class jumps to other JSPs for data presentation
Now with the STRUTS2, the implementation process becomes
1 JSP departure action
2 struts2 intercept request, invoke background action
3 Action Returns the result, presenting the data by different JSPs
Below we look at the need for the jar package
The first two are Apache Commons jar packages, ignoring
Freemarker offers another way of showing
OGNL provides a OGNL expression
Struts2-core provides STRUTS2 core package
Xwork-core because struts2 a lot of things based on webwork, so also need this core package
We have three JSPs available
Login Interface login.jsp
1 <%@ page language= "java" contenttype= "text/html; CHARSET=GBK "2 pageencoding=" GBK "%> 3 <% @taglib prefix=" s "uri="/struts-tags "%> 4
Login Success Interface welcome.jsp<%@ page language= "java" contenttype= "text/html; CHARSET=GBK " pageencoding=" GBK "%><% @taglib prefix=" s "uri="/struts-tags "%>
Login Failure Interface error.jsp<%@ page language= "java" contenttype= "text/html; CHARSET=GBK " pageencoding=" GBK "%><% @taglib prefix=" s "uri="/struts-tags "%>
When login.jsp triggers an action, it forwards the request to send Login.action, which is intercepted by the background and handed over to the action handler configured in Struts.xml1 <?xml version= "1.0" encoding= "GBK"?> 2 <! DOCTYPE struts Public 3 "-//apache software foundation//dtd struts Configuration 2.1.7//en" 4 "/http Struts.apache.org/dtds/struts-2.1.7.dtd "> 5 <struts> 6 <!--specify global internationalized Resource Files---7 <constant Name= "struts.custom.i18n.resources" value= "mess"/> 8 <!--Specify the character set used for internationalized encoding-- 9 <constant Name= "struts.i18n.encoding" value= "GBK"/>10 <!--all action definitions should be placed under the package-->11 <package name= "Test" extends= "Struts-default" >12 <action name= "Login" class= "com.test.action.LoginAction" >13 <result name= "error" >/error.jsp</result>14 <result name= "Success" >/welcome.jsp</result >15 </action>16 </package>17 </struts>
The following is Loginaction code, you can see the successful landing, the program to write username into the session. To make it easier for us to use ${sessionscope.user} in welcome.jsp to get a name1 package com.test.action; 2 3 import com.opensymphony.xwork2.ActionContext; 4 import com.opensymphony.xwork2.ActionSupport; 5 6 Public Class Loginaction extends Actionsupport {7 private string Username; 8 private string password; 9 Public St Ring GetUserName () {One return username;12 }13, public void Setusername (String username) { This.username = username;16 }17 public String GetPassword () { return password;20 }21 public void SetPassword (string password) { This.password = password;24 }25-Public String execute () Throws Exception { getusername () equals ("Xingoo") && GetPassword () equals ("123")) { Actioncontext.getcontext (). GetSession (). Put ("User", GetUserName ()); return success;30 }else{31 return error;32 }33 }34}
There are also two internationalized documents here,
Mess.propertiesLoginpage=loginpageerrorpage=errorpagesuccpage=succpagefailtip=sorry,login Failedsucctip=welcome{0},login Successuser=usernamepass=passwordlogin=login
Mess_zh_cn.propertiesLoginpage= Login Interface errorpage= failure Interface succpage= Successful interface failtip= Sorry, you can't login! Succtip= welcome, {0}, you are already logged in! user= User name pass= password login= Login
Login interfaceLanding successLogin failedFrom:http://www.cnblogs.com/xing901022/p/3961661.html
"Java Development Series"--struts2 simple Getting Started example