Struts Form Transfer Value
About three or four months without the struts frame, suddenly want to pick up, but found a lot of forgotten. There is a problem that the pass-through value cannot pass over. No way, the Internet to check a bit, saw a teacher's post, summed up very well. We hereby reprint and share the link at the end of this article.
In the first STRUTS2 program in Eclipse we built the first struts program, so how to pass the user name on the landing page to the successful login page?
There are three different ways to
1, use the default action delivery method.
2, customize a VO, use this VO in action
3, using the Modeldriven method.
described separately below.
1, use the default action delivery method.
The action file is as follows:
Package struts2.login;
public class Loginaction {
Private String username;
private String password;
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
Public String execute () {
System.out.println (LoginAction.class.hashCode ());
if (Username.equalsignorecase ("AAA") &&
Password.equals ("aaaaaa")) {
return "LOGINSUC";
}
else {
return "Loginfail";
}
}
}
The successful login files are as follows:
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%@ taglib uri= "/struts-tags" prefix= "s"%>
<meta http-equiv= "Content-type" content= "text/html;charset=gb2312" >
Welcome, <s:property value= "username"/> Login successful.
2, customize a VO, use this VO in action
Custom VO file name: Loginvo.java
File contents:
Package struts2.login;
public class Loginvo {
Private String username;
private String password;
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
}
In the action file, to use this VO
File contents:
Package struts2.login;
public class Loginaction {
Private LOGINVO user = null;
Public String execute () {
System.out.println (LoginAction.class.hashCode ());
if (User.getusername (). Equalsignorecase ("AAA") &&
User.getpassword (). Equals ("aaaaaa")) {
return "LOGINSUC";
}
else {
return "Loginfail";
}
}
Public Loginvo GetUser () {
return user;
}
public void SetUser (Loginvo user) {
This.user = user;
}
}
The successful login files are as follows:
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%@ taglib uri= "/struts-tags" prefix= "s"%>
<meta http-equiv= "Content-type" content= "text/html;charset=gb2312" >
Welcome, <s:property name= "User.username" > Login successful.
Note that part of the login file also needs to be modified
The contents of the file are as follows:
<meta http-equiv= "Content-type" content= "text/html;charset=gb2312" >
<title>login2</title>
<form action= "Login.action" method= "POST" >
Username:<input type= "Input" name= "User.username" ><br>
Password:<input type= "Input" name= "User.password" ><br>
<input type= "Submit" value= "Login" >
</form>
3, using the Modeldriven method.
Also need a VO, this Vo and Method 2 in the same, but the action in the wording is not the same.
The action file reads as follows:
Package struts2.login;
Import Com.opensymphony.xwork2.ModelDriven;
public class Loginaction implements modeldriven<loginvo>{
@Override
Public Loginvo Getmodel () {
TODO auto-generated Method Stub
return user;
}
Private Loginvo user = new Loginvo ();
Public String execute () {
System.out.println (LoginAction.class.hashCode ());
if (User.getusername (). Equalsignorecase ("AAA") &&
User.getpassword (). Equals ("aaaaaa")) {
return "LOGINSUC";
}
else {
return "Loginfail";
}
}
}
The successful landing page and login file do not need to append the user prefix, that is, the same as the contents of Method 1 file.
Summary of three methods:
The first method places the value of the form in the action file, and when the form submits a lot of items, the action content becomes much, very bloated. Available when the project is small.
The second method puts the value of the form separately in Vo, solves the problem of the action file bloated, and makes the form and action separate, better. However, it needs to be identified on the JSP pages that you set up and get.
The third method, on the basis of the second method, removes the set and get methods in the action by implementing a specific interface, and removes the identity on the JSP page. Makes logic clearer for background programs.
Link Address: http://blog.csdn.net/tianlincao/article/details/6098371
Struts Form Transfer Value