[Struts] struts2 the value between the Java class and the JSP display page to pass __ Large data

Source: Internet
Author: User

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





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.