I have a .jsp form like so
<s:form action="AuditLogReport">Source IP<br>
<input type="text" class="auditLogSearch" name="sourceIp" value="All">
<input type="submit" value="Submit"/>
</s:form>
And my struts.xml is defined
<action name="AuditLogReport"
class="com.mycom.selfservice.actions.AuditLogAction" method="auditLogReport">
<result name="success">jsp/AuditLog.jsp</result>
<result name="error">jsp/Error.jsp</result>
</action>
Here is my class definition
public class AuditLogAction extends ActionSupport implements Action,ServletRequestAware {
And in my AuditLogAction class there is a method
public String auditLogReport() {
System.out.println("Im in auditLogReport...");
but when I click the button, auditLogReport method does not get hit. What I see in the browser url is http://localhost:7001/BPSelfServicePortal/AuditLogReport.action
It is appending .action which I think is why it doesn't find the method. So I tried putting
<constant name="struts.action.extension" value=""/>
in the struts.xml. That prevented the .action from being appended but the button still didn't work. Plus it caused the .css and images from being found. I have a link that uses the default execute() method and that works ok.
If I simply remove the .action in the url and hit enter, it hits the method but then none of the values in the form get passed.
Suggestions?
The problem turned out to be a Date parameter. Apparently struts2 doesn't like them.
public Date getFromDate() {
return fromDate;
}
public void setFromDate(Date fromDate) {
this.fromDate = fromDate;
}
Changed it to
public String getFromDate() {
return fromDate;
}
public void setFromDate(String fromDate) {
this.fromDate = fromDate;
}
And then it worked!
Related
I have the class Lesson, which holds the reference to Course object, like so:
public class Lesson {
...
private Course course;
...
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
...
}
And I want to set the Course property on the Lesson object through the select form:
<form:form method="post" action="addLesson" modelAttribute="lesson">
<form:select path="course">
<form:options items="${courses}"/>
</form:select>
<input type="submit" name="addLesson" value="Add lesson">
</form:form>
In my controller I have the following:
#Controller
public class LessonController {
#Autowired
private LessonRepository lessonRepository;
#Autowired
private CourseRepository courseRepository;
// form setup
#RequestMapping(value = "/", method = RequestMethod.GET)
public String showSchedule(ModelMap model) {
...
model.addAttribute("lesson", new Lesson());
model.addAttribute("courses", courseRepository.findAll());
...
}
#RequestMapping(value = "/addLesson", method = RequestMethod.POST)
public String addLesson(#ModelAttribute("lesson") Lesson lesson, BindingResult result) {
lessonRepository.save(lesson);
return "redirect:/";
}
...
}
The problem is that it passes the String representation of the Course object (defined by toString()) to the course setter of the Lesson object.
How do I properly set the Course property of the Lesson object using my select form?
Usually for UI rendering Formatter<T> is used with ConversionService. But prior to Spring 3, PropertyEditors were used.
I've shared sample github project for your case https://github.com/jama707/SpringSelectBoxSample
#Component("courseFormatter")
public class CourseFormatter implements Formatter<Course> {
private CourseRepository courseRepository=new CourseRepository();
#Override
public String print(Course course, Locale arg1) {
return course.getName();
}
#Override
public Course parse(String actorId, Locale arg1) {
return courseRepository.getCourse(actorId);
}
}
According to spring documentation you need to set itemValue and itemLabelon the form:options tag, otherwise ,as you already mentioned, the value will be the toString() of the Object, itemValue and itemLable should refer to properties from your Course bean.
Assuming that your Course class has a property name, then your form should look like this:
<form:form method="post" action="addLesson" modelAttribute="lesson">
<form:select path="course">
<form:options items="${courses}" itemValue="name" itemLabel="name"/>
</form:select>
<input type="submit" name="addLesson" value="Add lesson">
</form:form>
You can bind the course object directly instead of some course object property by using Spring Converters.
Implement the Converter interface which in your case may convert selected courseName to Course:
public class CourseConverter implements Converter<String, Course> {
public Course convert(String source) {
List<Course> courseList = //Populate courseList the way you did in Lesson
Course course = //Get course object based on selected courseName from courseList;
return course;
}
}
Now register the converter:
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" >
<property name="converters">
<set>
<bean class="your.package.CourseConverter"/>
</set>
</property>
</bean>
and change your form:options as:
<form:options items="${courses}" itemValue="courseName" itemLabel="courseName"/>
Here i am chaining action then want to get data on last preview action
My First action call first.jsp. I Enter some value here I want to get that value on next Action call
<action name="first">
<result name="success">/first.jsp</result>
</action>
<action name="second" class="com.ved.action.FirstAction">
<result name="success">/second.jsp</result>
</action>
<action name="preview" class="com.ved.action.SecondAction">
<result name="success">/preview.jsp</result>
</action>
I want to display first.jsp data on preview.jsp
first.jsp
<s:form action="second">
<s:textfield name="username" label="UserName"/>
<s:submit name="next" value="Next"/>
</s:form>
second.jsp
<s:form action="preview">
<s:textfield name="address"/>
<s:submit name="priview" value="priview"/>
preview.jsp
<s:property value="username"/>
<s:property value="address"/>
i want display both page data on preview
my Action is as
public class FirstAction extends ActionSupport{
private String firstname;
public String execute() throws Exception {
return SUCCESS;
}
//setter getter for firstname
}
public class SecondAction extends ActionSupport{
private String address;
public String execute() throws Exception {
return SUCCESS;
}
//setter getter for address
}
HTTP is stateless.
Solution:
1) You have to use session to persist the data.
2) During transition from first to second, the data should be in second.jsp. Use html hidden element to store the data of first.jsp. So, you have to restore the data in request scope.
I have the following dropdown list which correctly shows the options but when I select an item and submit the form it runs into the following error :
'select', field 'list', name 'name': The requested list key 'listnames' could not be
resolved as a collection/array/map/enumeration/iterator type. Example: people or
people.{name} - [unknown location]
My JSP form
<s:form method="POST" action="addNames">
<s:select name="name"
label="Names"
list="listnames"
/>
</s:form>
My Action
#Action
public class Myaction implements ModelDriven{
private MyClass myclass = new MyClass();
private List listnames = new ArrayList();
#Override
public MyClass getModel() {
return this.myclass;
}
public List getListnames() {
return listnames;
}
public void setListnames(List listnames) {
this.listnames = listnames;
}
public MyClass getMyClass() {
return myclass;
}
public void setMyClass(MyClass myclass) {
this.myClass = myclass;
}
}
My Class
public class MyClass {
private String name;
..... getter and setters go here ....
}
struts
<package name="MyUsers" extends="default" namespace="/MyUsers">
<action name="*" method="{1}" class="com.myproject.controller.Myaction">
<result name="uAdd" type="tiles" >uAdd</result>
<result name="uView" type="tiles" >uView</result>
</action>
</package>
STEP 1 :make sure that getter and setter for listnames are done properly
STEP 2 :make sure that you have done the declaration and initialization for listnames List properly
UPDATE 2
Sample Example
struts.xml
<action name="getText" class="commonpackage.ReportsCommonClass" method="getText">
<result name="success">index.jsp</result>
</action>
<action name="myaction" class="commonpackage.ReportsCommonClass" method="myaction">
<result name="success">index2.jsp</result>
</action>
index.jsp
<s:form id="conform" action="myaction" method="post">
<label>NAME</label>
<s:select id="name1" name="name1" list="mylist" headerKey="0" headerValue="--SELECT--"/>
<s:submit value="Click" />
</s:form>
In commonpackage.ReportsCommonClass class
ArrayList mylist=new ArrayList();
public ArrayList getMylist() {
return mylist;
}
public void setMylist(ArrayList mylist) {
this.mylist = mylist;
}
public String getText()
{
mylist.add("NAME 1");
mylist.add("NAME 2");
mylist.add("NAME 3");
mylist.add("NAME 4");
mylist.add("NAME 5");
return SUCCESS;
}
String name1;
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
public String myaction()
{
System.out.println("NAMEEEEEEEEEEEEEEEEEEEEE:"+name1);
return SUCCESS;
}
change your select tag like this
<s:form method="POST" action="addNames">
<s:select name="myclass.name"
label="Names"
list="listnames"
/>
</s:form>
Edit:
Problem:
I guess you are hitting jsp directly hence there is no any action execution. If there is no any action execution then there is no any list in request.
Solution.
Hit URL in way that action class get executed and list should initialize or populated before rendering the jsp or view.
Create a method like populateView in action class and execute this method rather than directly execution JSP.
Hope you understand what I want to say.
Provide getter setter for name in your action class.
As your select tag name is name <s:select name="name"> when you submit your form it will search for the property name in your action class.
This may be the problem in your case
i'm trying to get a property value from a bean object inside jsp using standard actions,the initial html form go to a servlet which sets a value inside the desired property,sets the attribute inside the request object and then forwards it to the jsp page, the jsp gets the value from the property by using standard actions but it gets null!:
the bean object:
public class dog {
private String bread;
public String getBread() {
return bread;
}
public void setBread(String bread) {
this.bread = bread;
}
}
the servlet:
dog d=new dog();
d.setBread("Kizer");
request.setAttribute("bread", d);
RequestDispatcher view=request.getRequestDispatcher("index.jsp");
view.forward(request, response);
the JSP (index):
< id="person" class="com.example.model.dog" scope="request" />
Person created by servlet: <jsp:getProperty name="person" property="bread" />
why it returns null ?
In your JSP use
<jsp:useBean id="bread" class="com.example.model.dog" scope="request" />
<jsp:getProperty name="bread" property="bread" />
Using bean id atrribute is the same as bean object.
I would like to know how to use jsp:setProperty in the following scenario. Here is a simple example of two java classes.
public class MyExample {
private MyName myNameExample = new MyName();
public MyExample() {}
public MyName getMyNameExample() {
return myNameExample;
}
public void setMyNameExample(MyName setTo) {
myNameExample = setTo;
}
}
public class MyName {
private String firstName;
public MyName() {}
public String getFirstName() {
return firstName;
}
public String setFirstName(String setTo) {
firstName = setTo;
}
}
I was trying to use something like:
<jsp:useBean id="example" class="MyExample" scope="page"/>
<jsp:setProperty name="example" property="????" value="aFirstName"/>
The important part here is that I want to reference the MyName object from within MyExample. Therefore, creating a bean to directly access MyName will not help me. So I am not looking for this answer:
<jsp:useBean id="name" class="MyName" scope="page"/>
<jsp:setProperty name="name" property="firstName" value="aFirstName"/>
You could just create both beans and set one in other by ${}.
<jsp:useBean id="myName" class="MyName" scope="page" />
<jsp:setProperty name="myName" property="firstName" value="aFirstName" />
<jsp:useBean id="myExample" class="MyExample" scope="page" />
<jsp:setProperty name="myExample" property="myExampleName" value="${myName}" />
Unrelated to the concrete problem, I'd suggest to invest time in learning servlets and MVC. The above is a pretty old fashioned and tight-coupled way of controlling the models in the view.
Note that using packageless classes may not work in all circumstances (since they are invisible for normal classes inside a package). Only in certain Apache Tomcat configurations it will work. Rather put your classes inside a package in order to be not dependent of that.