2012.09.10 02:52 Update: Here is a more troublesome example, is that they have just learned to write struts2, using the Modeldriven<e> interface, interested can download a reference.
Download Address: http://download.csdn.net/detail/kaulctin/4559588
---Split line--
In the servlet, parameter passing relies on the plug-in parameters in request, session, application, or Jsp:param, which is troublesome. In Struts2, simply set the GetXXX () in the Java class, the Setxxx () method can be used in the Java class and the display of the page easy to pass parameters, if the implementation of the Modeldriven<e> interface, you can make the passed parameters more specification.
For Example: There is a login page for login.jsp,body content as follows:
<%@ taglib prefix= "s" uri= "/struts-tags"%> <s:form name=
"frm" action= "Login_do" method= "POST" >
<s:textfield name= "name" label= "user"/> <s:password name= "pwd" label= "
password"/> <s:submit value=
" Login "/>
</s:form>
Configure the action in Struts.xml as follows:
<action name= "Login_do" class= "com.study.ImpDao.Imp" method= "Login" >
<result name= "Success" > main.jsp</result>
<result name= "error" >error.jsp</result>
</action>
There are string variable name, pwd, and setxxx () methods in the Imp class to get the parameters passed from the Web page.
public void SetName (String name) {
this.name = name;
}
public void SetPwd (String pwd) {
this.pwd = pwd;
}
This allows you to use the passed value in the method login Login_do this action call. For simplicity, set login () as follows:
Public String Login () {
System.out.println (name);
System.out.println (PWD);
Return "Success";
}
When the user clicks on the "Login" button in login.jsp to submit the form, it prints out the name passed over in the background console, pwd two parameter values. The server side then responds to the Main.jsp page to the user, and if you continue to use name, pwd values in main.jsp, you will need to write the GetXXX () method in the Imp class in the following format:
Public String GetName () {return
this.name;
}
Public String setpwd () {return
this.pwd;
}
The main contents of the set main.jsp are as follows:
<%@ taglib prefix= "s" uri= "/struts-tags"%>
<p> Username: <s:property value= "name"/>
<p> Password: <s:property value= "pwd"/>
<!--This can also be obtained by using the request object to obtain the parameters of the two parameters-->
<%= Request.getparameter ("name")%>
<%=request.getparameter ("pwd")%>
You can display the two parameters of the white submission.
Actual effect:
1 login Page
2 Click to log in after submitting the form, console output
3 Java class returns success jumps to main.jsp and renders to the user page