user1191027
user1191027

Reputation:

struts2 actions - Multiple Methods & Multiple classes

How do you create an action in struts2 that uses multiple methods in different classes?

So for example I would like something like this but I'm pretty sure this doesn't work:

<action name="person_create" method="personCreate, carCreate" class="PersonActionBean, CarActionBean">
   <result name="success">index.jsp</result>
</action>

To be specific, I'm using struts 2.1.8.1

PersonAction.java - personCreate():

Person person = new Person();

CarAction.java - carCreate():

Car car = new Car();

Upvotes: 1

Views: 5630

Answers (4)

Jaiwo99
Jaiwo99

Reputation: 10017

There are two variable:

1: why are you willing to call 2 actions, from the concept of MVC, you shouldn't do this, if you wanna process to different objects, you should build a new layer(e.g. interface, or service layer) to do this.

2: if you have to do this, you can define a new action, which using the chain result type to call them all. here is the Chain result type, but be careful.

Upvotes: 2

Aliaksei Bulhak
Aliaksei Bulhak

Reputation: 6208

or you can make something like this

public class MainAction extends ActionSupport{

private Car car;
private Persone persone;

public String execute() {
 }

// add getters and setters to car and persone objects.
}

And your jsp will be look like this:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>
<body>
<s:form action="init">
    <s:textfield key="car.name" name="car.name"/>
    <s:textfield key="car.year" name="car.year"/>
    <s:textfield key="car.name" name="car.name"/>
    <s:textfield key="car.year" name="car.year"/></s:textarea>
    <s:submit key="init.submit"/>
</s:form>
</body>
</html>

and your struts.xml file:

<action name="init" method="execute" class="MainAction">
   <result name="success">index.jsp</result>
</action>

and when you submit form your action class will be contain persone and car objects with info from form))

Upvotes: 1

nmc
nmc

Reputation: 8696

You're right, it doesn't work that way. Consider if one action returned success and another action returned error. Which result would the struts2 framework execute in that case?

Instead what you can do is call one action method that will call the other two methods within it. Something like this:

<action name="person_create" method="execute" class="MainAction">
   <result name="success">index.jsp</result>
</action>

And as part of that action, you can call your other methods, like:

public String execute() {
    Person person = new Person();
    Car car = new Car();
    // Add any additional logic and return appropriate value
}

If the above isn't an appropriate solution, please edit your question with more details as to why you want to call two separate methods in your action. It is still unclear to me why you would want the two methods for one action in that way.

Upvotes: 1

Aliaksei Bulhak
Aliaksei Bulhak

Reputation: 6208

If I understand you clearly you want to call 2 action classes when call persone_create action. And can you show me this action classes. If this classes having fields with the same name it will be a problem. Because all field from action classes (when action class is called) go to the ValueStack and in result only one of this classes will be having all fields with information from your jsp page.

Upvotes: 1

Related Questions