Example of submitting multiple requests in a form in struts2 (multiple submit buttons) _jsp programming

Source: Internet
Author: User

Using this method also requires the request parameter to specify the action to be performed. The request parameter name is in the format
Action!method.action
Note: Since Struts2 only requires a parameter name, the parameter value is anything.

Here's an example program that shows how to handle a form with multiple submit:

Main Page more_submit.jsp

Copy Code code as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%>
<%@ taglib prefix= "s" uri= "/struts-tags"%>
<title>my JSP ' hello.jsp ' starting page</title>
<body>
<s:form action= "Submit.action" >
<s:textfield name= "msg" label= "Input"/>
<s:submit name= "Save" value= "saved" align= "left" method= "save"/>
<s:submit name= "print" value= "Printing" align= "left" method= "print"/>
</s:form>
</body>

Moresubmitaction.java

Copy Code code as follows:

Package action;

Import javax.servlet.http.*;
Import Com.opensymphony.xwork2.ActionSupport;
Import org.apache.struts2.interceptor.*;

public class Moresubmitaction extends Actionsupport implements
Servletrequestaware {
Private String msg;
private Javax.servlet.http.HttpServletRequest request;

Get HttpServletRequest Object
public void Setservletrequest (HttpServletRequest request) {
This.request = Request;
}

Action to handle the Save Submit button
Public String Save () throws Exception {
Request.setattribute ("Result", "successfully saved [" + msg + "]");
return "Save";
}

Action to handle Print submit button
Public String print () throws Exception {
Request.setattribute ("Result", "successful print [+ msg +]");
return "print";
}

Public String getmsg () {
return msg;
}

public void Setmsg (String msg) {
this.msg = msg;
}
}

Structs.xml

Copy Code code as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd ">
<struts>

<package name= "Demo" extends= "Struts-default" >
<action name= "Submit" class= "action. Moresubmitaction ">
<result name= "Save" >
/result.jsp
</result>
<result name= "Print" >
/result.jsp
</result>
</action>
</package>


</struts>

result.jsp

Copy Code code as follows:

<%@ page language= "java" contenttype= "text/html; CHARSET=GBK "
pageencoding= "GBK"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=iso-8859-1 ">
<title> Submit Results </title>
<body>
</body>

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.