Several ways to take action on a JSP page are worth several methods __js

Source: Internet
Author: User

We usually need to pass the backend values to the JSP page in the action, we used the more traditional method to use El expressions such as: ${requestscope.value}, but, the business is changeable, we have to pass the value to the JSP page in another way, has reached the effect we want.
1. Previously Yoyo in class: Servletactioncontext.getrequest (). setattribute ("Key", "value"), put it in the request first, then go to the page with ${ Requestscope.key} This kind of El expression is also a method.

2. You can directly define the properties to pass into the foreground in the action, and then the getter and setter like:

public class Testaction extends Actionsupport {


Public String Execute () throws Exception {

Userinfo=this.userinfodao.getuserinfo ();

System.out.println (Userinfolist.get (0). Get ("user_id"));

Return "Success";

}

Private Userinfo Userinfo;


Getter ...

Setter......

}

This is the foreground can go directly to the UserInfo properties, you do not have to servletactioncontext.getrequest in action (). setattribute ("UserInfo", userinfo);

Then go to the page with El expressions <input type= "text" value= "Userinfo.userid" > UserID values into a text box.

3. We also have a way of passing values from the action to the JSP page: the Map object. The Map object is a form of key-value pairs, and we first define an attribute (same 2) in the action:


public class Testaction extends Actionsupport {


Public String Execute () throws Exception {

Collection form

Userinfomaplist=operatedbbyjdbc.select ("SELECT * from UserInfo");

Single Data form

Userinfomap=operatedbbyjdbc.select ("select * from UserInfo where user_id=1");

Return "Success";

}

Collection form

Private list<map<string,string>> userinfomaplist;

Single Data form

Private map<string,string> Userinfomap;


Getter ...

Setter......


Getter ...

Setter......


}

The Operatedbbyjdbc.select () method above me is a method of using SQL statements to make JDBC queries, in which I return the query data to the form of a map key value pair, by the way, Sometimes we hql business in the SSH framework, we can consider using two forms of data access in the framework of the project, HQL (Hibernate) and SQL (JDBC), which is also easy to develop.

The above point (2nd) says that once you define an attribute, then the getter and setter can be used directly on the page, but how does the map object take value in the JSP?

<a a single data form: In the form of ${user_account}, because in the JDBC query process, put the column name in the Map object's key, and then put the corresponding column values in the Walue, the page directly with the column name plus El expression can be taken out of the corresponding value value.

<b Collection Object Form: Collection object to traverse, we want to use the core tag library <c:forech> or struts2 tag library <s:s:iterator>

Whatever it is you are happy with.

<c:forech> (Jstl tag) traverses a Map object:


<c:foreach items= "${userinfomaplist}" var= "UserInfo" varstatus= "Userinfos" >

${Userinfomaplist} The map collection to traverse

${userinfo. USER_ID}//var. Column name

${userinfo. User_account}

</c:forEach>

<s:iterator> (struts2 tag) traverse a map:


<s:iterator value= "userinfolist" id= "user" status= "users" >

${user_account}

${USER_ID}

</s:iterator>

Notice the difference between the two.

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.