Hi guys new to spring and was wondering why this does work like I though it would. So I have a project model which has a name(a string) and a user(a user object). So I store it mysql and want to retrieve a project base on those two fields and i want to display the project on page which I did already(but only base on user). So add a new thing where I'm using a search bar for input for name. So when the page first load, there isn't any input so it should just retrieve all projects base of user. So I made name (required = false). However, it doesn't show all the projects only base on user(Nothing shows). It only something shows when I enter like a name into search bar.
<form class="form-inline mt-5 my-lg-0" action="/myWork">
<input type="text" class="form-control" name="name" placeholder="Search Project" />
<input type="submit" value="Search" class="btn btn-primary"/></form>
public interface ProjectRepository extends JpaRepository<Project, Integer> {
#Override
List<Project> findAll();
public List<Project> findByUser(User user);
public List<Project>findByUserAndName(User user,String name);
public List<Project> findByType(String type);
}
//Get a project base on user and project name
public List<Project> getProjectByUserandName(Authentication authentication,String name) {
User user = authenticationService.getPrincipal(authentication);
return projectRepo.findByUserAndName(user,name);
}
#GetMapping("/myWork")
public ModelAndView showUserProject(Authentication authentication, #RequestParam(required = false) String name) {
ModelAndView modelAndView = new ModelAndView();
List<Project> projects = new ArrayList<Project>();
try {
projects = projectService.getProjectByUserandName(authentication,name);
System.out.println(projects);
Collections.sort(projects, new customComparator());
} catch (Exception e) {
e.printStackTrace();
}
modelAndView.addObject("projects", projects);
return modelAndView;
}
Handle Empty case of name in getProjectByUserandName.
Instead of
return projectRepo.findByUserAndName(user,name);
Use this :
if(StringUtils.isEmpty(name))
return projectRepo.findByUser(user);
else
return projectRepo.findByUserAndName(user,name);
Related
I'm writing my first spring boot app and I'm stuck with this problem. I can't show error message to user. Object without that data is not saved in the database and that is OK. But showing error message is the problem. When I debug i get errors size = 0
This is model
#Size(min = 1, message = "Address is invalid.")
#NotNull
#Column
private String address;
Controller
#RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String createNewBusiness(#Valid #ModelAttribute("business") Business business,
BindingResult result, Model model) {
model.addAttribute("userEmail", getUserEmail());
logger.info("/business/create:" + business.toString());
LocationResponse locationResponse = geoService.getCoords(business.getAddress());
if (locationResponse.getStatus().equals("OK")) {
business.setLatitude(locationResponse.getResults().get(0).getGeometry().getLocation().getLat());
business.setLongitude(locationResponse.getResults().get(0).getGeometry().getLocation().getLng());
business.setUserId(getUserId());
businessService.createNew(business);
model.addAttribute("business", business);
} else {
business.setAddress(null);
model.addAttribute("business", business);
}
if (result.hasErrors()) {
List<FieldError> errors = result.getFieldErrors();
for (FieldError error : errors ) {
System.out.println (error.getObjectName() + " - " + error.getDefaultMessage());
}
return "newBusiness";
}
return "business";
}
Thymeleaf
<div class="input-field left m-0 w-100">
<i class="fa fa-map-marker prefix grey-text" aria-hidden="true"></i>
<input placeholder="Address" id="inputAddress" name="address" type="text" class="validate my-0" th:required="true">
<label th:errors="*{address}" th:if="${#fields.hasErrors('address')}" >Invalid address</label>
</div>
Did you define a Validator in your #SpringBootApplication?
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
#Bean
public Validator validator() {
return new LocalValidatorFactoryBean();
}
}
Here I have created dynamic dropdown list using this link, but when I select some value from available list it should be called in action class.
The dropdown list which can be seen in the image ,here the values are loaded dynamically from the database and now what I want is when I select any value from that two dropdown list that values (I mean text value) should be sent to the action class and there I will execute one JDBC select query on the basis of this two values and will display in the table shown in the image but everything should be on load.Action should be on selecting values from dropdown list not on any button click .With static values I am able to call value from dropdown list into action class with name attribute.But in this case I cannot :(
I hope I am clear now .
I have tried calling select tag using listkey,name and id but none of them worked .
Below is my JSP code:
<div>
<div class="invoicetext1">Event Name :</div>
<s:select name="dp.eventState"
list="%{state}"
class="billlistbox1"
id="eventName" />
<div>
<s:select name="dp.companyState"
class="billlistbox2"
listKey="companyState"
list="%{status}">
</s:select>
</div>
<div class="invoicetext2">Company Name :</div>
<div class="clear"></div>
</div>
<s:form action="ActionSelect">
<s:submit value=" Click Here"/>
</s:form>
<div>
Action class for loading dynamic dropdown list :
package com.ca.actions;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.ca.database.Database;
import com.ca.pojo.Event;
import java.sql.PreparedStatement;
import com.opensymphony.xwork2.ActionSupport;
public class RetrieveEvNaCoNaAction extends ActionSupport {
private static final long serialVersionUID = -5418233715172672477L;
List<Event> dataForBillsJspList;
private List state = new ArrayList();
private List status = new ArrayList();
String eventName;
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public RetrieveEvNaCoNaAction() {
// TODO Auto-generated constructor stub
}
public List<Event> getDataForBillsJspList() {
return dataForBillsJspList;
}
public void setDataForBillsJspList(List<Event> dataForBillsJspList) {
this.dataForBillsJspList = dataForBillsJspList;
}
public List getStatus() {
return status;
}
public void setStatus(List status) {
try {
Database database = new Database();
Connection con = database.Get_Connection();
PreparedStatement ps = con
.prepareStatement("SELECT EVENT_NAME,COMPANY_NAME,date_format(FROM_DATE,'%d/%m/%Y') as dateAsFrom,date_format(TO_DATE,'%d/%m/%Y') as dateAsTo FROM EVENT");
ResultSet rs = ps.executeQuery();
//dataForBillsJspList = new ArrayList<Event>();
while (rs.next()) {
/*dataForBillsJspList.add(new Event(rs.getString("EVENT_NAME"),
rs.getString("COMPANY_NAME"), rs
.getString("dateAsFrom"), rs
.getString("dateAsTo")));
System.out.println(rs.getString("EVENT_NAME"));*/
status.add(rs.getString("COMPANY_NAME"));
}
System.out.println("Data Collected ...");
}catch(Exception e)
{
e.printStackTrace();
}
}
public List getState() {
return state;
}
#Override
public String execute() throws Exception {
// TODO Auto-generated method stub
setState(this.state);
setStatus(this.status);
return "success";
}
public String showEventDetails(){
System.out.println("Hi.."+eventName);
return SUCCESS;
}
public void setState(List state) {
//implement the application specific logic to
try {
Database database = new Database();
Connection con = database.Get_Connection();
PreparedStatement ps = con
.prepareStatement("SELECT EVENT_ID,EVENT_NAME,COMPANY_NAME,CONTACT_PERSON,CONTACT_NO,EMAIL_ID,EVENT_VENUE,date_format(FROM_DATE,'%d/%m/%Y') as dateAsFrom,date_format(TO_DATE,'%d/%m/%Y') as dateAsTo ,EVENT_TIME FROM EVENT");
ResultSet rs = ps.executeQuery();
dataForBillsJspList = new ArrayList<Event>();
while (rs.next()) {
dataForBillsJspList.add(new Event(rs.getString("EVENT_ID"),rs.getString("EVENT_NAME"),
rs.getString("COMPANY_NAME"),rs.getString("CONTACT_PERSON"),rs.getString("CONTACT_NO"),rs.getString("EMAIL_ID"),rs.getString("EVENT_VENUE"), rs
.getString("dateAsFrom"), rs
.getString("dateAsTo"),rs.getString("EVENT_TIME")));
//System.out.println(rs.getString("EVENT_NAME"));
state.add(rs.getString("EVENT_NAME"));
System.out.println(rs.getString("EVENT_ID"));
}
System.out.println("Data Collected ...");
}catch(Exception e)
{
e.printStackTrace();
}
//Here for displaying the data on UI, we are using few hardcoded values//
}
}
After loading dynamic dropdown list now i am trying to call selected value in action class by S.O.P but it gives null pointer exception. Below is my POJO class:
package com.ca.pojo;
public class Dropdown
{
private String eventState;
private String companyState;
public Dropdown() {
// TODO Auto-generated constructor stub
}
public String getEventState() {
return eventState;
}
public void setEventState(String eventState) {
this.eventState = eventState;
}
public String getCompanyState() {
return companyState;
}
public void setCompanyState(String companyState) {
this.companyState = companyState;
}
}
and below is action class where I am trying to call that selected value by using name attribute :
package com.ca.actions;
import com.ca.pojo.Dropdown;
import com.opensymphony.xwork2.ActionSupport;
public class DropdownAction extends ActionSupport
{
Dropdown dp;
public DropdownAction() {
// TODO Auto-generated constructor stub
}
public Dropdown getDp() {
return dp;
}
public void setDp(Dropdown dp) {
this.dp = dp;
}
#Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println(dp.getEventState());
return "success";
}
}
struts.xml is properly configured. Now after selecting two values I want to display data in the below table accordingly without any button click but in jsp i have created button just to see whether i am getting the selected value in action class but in actual i want it without any button click.
Well, there is a huge mess here :D
First of all, the NullPointerException is thrown because the values are not sent, and the values are not sent because they're not in the form.
You should enclose them in the form like this for them to be sent to the ActionSelect action:
<s:form action="ActionSelect">
<div class="invoicetext1">Event Name :</div>
<s:select name="dp.eventState"
list="%{state}"
class="billlistbox1"
id="eventName" />
<div>
<s:select name="dp.companyState"
class="billlistbox2"
listKey="companyState"
list="%{status}">
</s:select>
</div>
<div class="invoicetext2">Company Name :</div>
<div class="clear"></div>
</div>
<s:submit value=" Click Here"/>
</s:form>
Solved the mistery, this doesn't solve your problem, though.
You have two main ways to contact actions from a page:
Using a standard submit (as you're doing):
you either submit a form with its content, or call a link by eventually passing parameters in the querystring. This creates a Request, that will contact an action, that will return an entire JSP, that will be loaded in place of the page you're on now.
Using AJAX:
you POST or GET to an action without changing the current page, and the action can return anything, like a JSP snippet, a JSON result, a binary result (through the Struts2 Stream result), etc...
You then can choose what to do with the returned data, for example load it inside a <div> that before was empty, or had different content.
Now your problem is that you're contacting an action that is not the one you're coming from (is not able to re-render the entire JSP you're on) and you're calling it without using AJAX, then whatever the object mapped to the "success" result is (the whole JSP, or a JSP snippet), it will be loaded in place of the JSP you're on, and it will fail.
Since you seem to be quite new to this, I suggest you start with the easy solution (without AJAX), and after being expert with it, the next time try with AJAX.
That said,
avoid putting logic in getters and setters;
avoid calling methods that are not setter as setters (setState, setStatus...);
always make your attributes private;
try giving speaking names to variables: state and status for event states and company states are really confusing; and what about "state" instead of "name" (in jsp and on DB is "name");
consider loading informations like selectbox content in a prepare() method, so they will be available also in case of errors;
you're not closing the connections (and BTW it would be better to use something more evoluted, like Spring JDBC, or better Hibernate, or even better JPA, but for now keep going with the raw queries)
The following is a refactoring of your code to make it achieve the goal. I'll use #Getter and #Setter only for syntactic sugar (they're Lombok annotations, but you keep using your getters and setters, it's just for clarity):
<head>
<script>
$(function(){
$("#event, #company").on('change',function(){
$("#myForm").submit();
});
});
</script>
</head>
<body>
<form id="myForm">
<div>
...
<s:select id="event" name="event" list="events" />
...
<s:select id="company" name="company" list="companies" />
...
</div>
</form>
<div>
...
Table - iterate **dataForBillsJspList** here
...
</div>
</body>
public class RetrieveEvNaCoNaAction extends ActionSupport {
private static final long serialVersionUID = -5418233715172672477L;
#Getter private List<Event> dataForBillsJspList = new ArrayList<Event>();
#Getter private List<String> events = new ArrayList<String>();
#Getter private List<String> companies = new ArrayList<String>();
#Getter #Setter private String event = null;
#Getter #Setter private String company = null;
#Override
public void prepare() throws Exception {
Connection con;
try {
con = new Database().Get_Connection();
// load companies
PreparedStatement ps = con.prepareStatement("SELECT DISTINCT company_name FROM event");
ResultSet rs = ps.executeQuery();
while (rs.next()) { companies.add(rs.getString("company_name")); }
// load events
ps = con.prepareStatement("SELECT DISTINCT event_name FROM event");
rs = ps.executeQuery();
while (rs.next()) { events.add(rs.getString("event_name")); }
} catch(Exception e) {
e.printStackTrace();
} finally {
con.close();
}
}
#Override
public String execute() {
Connection con;
try {
con = new Database().Get_Connection();
// load the table. The first time the table is loaded completely
String sql = "SELECT EVENT_ID, EVENT_NAME, COMPANY_NAME, CONTACT_PERSON, CONTACT_NO, EMAIL_ID, EVENT_VENUE, " +
"date_format(FROM_DATE,'%d/%m/%Y') as dateAsFrom, date_format(TO_DATE,'%d/%m/%Y') as dateAsTo ,EVENT_TIME " +
"FROM event";
String where = "";
// if instead this action has been called from the JSP page,
// the result is filtered on event and company:
if (event!=null && company!=null) {
where = " WHERE event_name = ? AND company_name = ?";
}
// load companies
PreparedStatement ps = con.prepareStatement(sql + where);
if (where.length()>0) {
ps.setString(1,event);
ps.setString(2,company);
}
ResultSet rs = ps.executeQuery();
while (rs.next()) {
dataForBillsJspList.add(new Event(rs.getString("EVENT_ID"),rs.getString("EVENT_NAME"),rs.getString("COMPANY_NAME"),
rs.getString("CONTACT_PERSON"),rs.getString("CONTACT_NO"),rs.getString("EMAIL_ID"),
rs.getString("EVENT_VENUE"), rs.getString("dateAsFrom"), rs.getString("dateAsTo"),
rs.getString("EVENT_TIME")));
}
} catch(Exception e) {
e.printStackTrace();
} finally {
con.close();
}
return SUCCESS;
}
}
It is a kickoff example, but it should work.
The next steps are:
create a POJO with id and description, show the description in the select boxes, but send the id
use header values ("please choose an event"...) and handle in action conditional WHERE (only company, only event, both)
PAGINATION
Good luck
Using Javascript/jQuery you can do this, it depends on what you want to do after reached action class.
If you want to navigate to another page use the code below.
Add onchange event as an attribute to your dropdown,
onchange="customFunction(this.value)"
create customFunction in header part,
function customFunction(selectedValue){
window.location="Action_URL?myValue="+selectedValue;
}
Or if you want to return back the same page use jQuery ajax,
$("#eventName").change(function(e){
var selectedValue = $(this).val();
$.ajax({
type : 'post',
url : 'Action_URL',
data: { myValue: selectedValue},
success : function(data) {
alert(data);
console.log(data);
}
});
});
Hope this helps.
I am learning how to write REST APIs! How can I store form values into a text file, using rest calls?
index.html
<form action="rest/product/adddata" method="post">
Enter Id:<input type="text" name="id"/><br/><br/>
Enter Name:<input type="text" name="name"/><br/><br/>
Enter Price:<input type="text" name="price"/><br/><br/>
<input type="submit" value="Add Product"/>
</form>
service.java
#Path("/product")
public class ProductService{
#POST
#Path("/adddata")
public Response addUser(
#FormParam("id") int id,
#FormParam("name") String name,
#FormParam("price") float price) {
return Response.status(200)
.entity(" Product added successfuly!<br> Id: "+id+"<br> Name: " + name+"<br> Price: "+price)
.build();
}
}
I want to add value of id, name and price to a file. Where do I need to write function for adding data to file?
write code into
public Response addUser(
#FormParam("id") int id,
#FormParam("name") String name,
#FormParam("price") float price) {
saveFile(id,name,price);
return Response.status(200)
.entity(" Product added successfuly!<br> Id: "+id+"<br> Name: " + name+"<br> Price: "+price)
.build();
}
write code into saveFile to save into file
The best approach for doing this will be to create another class which can write and read from files. And inject that class in this rest endpoint.
Because The direct code in rest endpoint class should be to handle request not to write files
class FormSynchronizer {
public static final File FORM_BASE = new File(...); // The location of directory which will contain all these files
public void storeFile(Map map, String fileName){
File toStoreFile = new File(FORM_BASE, fileName);
/* Write code to store file */
}
}
Inject this class in your rest endpoint class
public class ProductService{
#Inject FormSynchronizer formSynchronizer;
#POST
#Path("/adddata")
public Response addUser(
#FormParam("id") int id,
#FormParam("name") String name,
#FormParam("price") float price) {
Map<String, Object> data = new HashMap<>();
data.put("id", id);
/* put all data you want to store in this map */
formSynchronizer.storeForm(data, "FORM_" + new Date().getTime()); // I used current time prefixed with 'FORM_' string as file name
return Response.status(200)
.entity(" Product added successfuly!<br> Id: "+id+"<br> Name: " + name+"<br> Price: "+price)
.build();
}
}
Use jackson to Convert Map Object to JSON file and vice-versa.
map to / from json file
I am trying to create a login feature for my apache tapestry website, where after logging in, instead of the 'Log In' and 'Register' button, the email of the logged user should be displayed, along with a 'Log Out' button.
Could anyone please tell how should this be implemented the best way?
I can't seem to figure out how should i detect if the user is logged in, in the frontend part, in order to display a different menu options (i am new in tapestry).
Best regards, Marius.
Authentication (of which login is a part) is very application specific. How you define a User (or do you call it a "Customer", for example) is not something the framework does.
Typically, you will have a SessionStateObject representing your User. You can then use something like this in your layout:
<t:if test="user">
<t:logoutLink/>
<p:else>
<t:signInForm/>
</t:if>
Again, components LogoutLink and SignInForm are for you to implement.
The user may be exposed from the Java code as:
#Property
#sessionState(create=false)
private User user;
This says that the user field is linked to a value stored in the HTTP session; further, the User will not be created when the field is first read; instead, your SignInForm component should assign to its user field.
After a little bit of research regarding this matter, i found the following approach:
1) I created an Authenticator interface
public interface Authenticator {
Users getLoggedUser();
boolean isLoggedIn();
void login(String email, String password) throws AuthenticationException;
void logout();
}
2) Also created an AuthenticatorImpl.java class that implements that interface
public class AuthenticatorImpl implements Authenticator {
public static final String AUTH_TOKEN = "authToken";
#Inject
private StartDAO dao;
#Inject
private Request request;
public void login(String email, String password) throws AuthenticationException
{
Users user = dao.findUniqueWithNamedQuery("from Users u where u.Email = '" + email + "' and u.Password = '" + password + "'");
if (user == null) { throw new AuthenticationException("The user doesn't exist"); }
request.getSession(true).setAttribute(AUTH_TOKEN, user);
}
public boolean isLoggedIn()
{
Session session = request.getSession(false);
if (session != null) { return session.getAttribute(AUTH_TOKEN) != null; }
return false;
}
public void logout()
{
Session session = request.getSession(false);
if (session != null)
{
session.setAttribute(AUTH_TOKEN, null);
session.invalidate();
}
}
public Users getLoggedUser()
{
Users user = null;
if (isLoggedIn())
{
user = (Users) request.getSession(true).getAttribute(AUTH_TOKEN);
}
return user;
}
}
3) Created the corresponding binding in the AppModule.java class
public static void bind(ServiceBinder binder)
{
binder.bind(StartDAO.class, StartDAOImpl.class);
binder.bind(Authenticator.class, AuthenticatorImpl.class);
}
4) And on my Layout.java page i used it in the following way
#Property
private Users user;
#Inject
private Authenticator authenticator;
void setupRender()
{
if(authenticator.getLoggedUser().getAccountType().equals("Administrator")){
administrator = authenticator.getLoggedUser();
}
user = authenticator.getLoggedUser();
}
Object onLogout(){
authenticator.logout();
return Login.class;
}
Layout.tml
<t:if test="user">
<span class="navbar-right btn navbar-btn" style="color: white;">
Welcome ${user.Name}! <a t:type="eventLink" t:event="Logout" href="#">(Logout)</a>
</span>
</t:if>
<t:if negate="true" test="user">
<span class="navbar-right">
<t:pagelink page="user/create" class="btn btn-default navbar-btn">Register</t:pagelink>
<t:pagelink page="user/login" class="btn btn-default navbar-btn">Sign in</t:pagelink>
</span>
</t:if>
This worked for me without any problems. Hope that it helps others.
Best regards, Marius.
I've got a form with a dropdown:
<div class="form-group">
<form:label path="departments">Dept. Code</form:label>
<form:select path="departments" items="${departmentMap}" multiple="true" />
departmentMap comes from the controller method:
#RequestMapping(value = "/officeForm", method=RequestMethod.GET)
public ModelAndView showOfficeForm() {
ModelAndView result = new ModelAndView("officeForm", "command", new Office());
List<Department> departmentsToDisplay = departmentServiceImpl.findAll();
Map<Department, String> departmentMap = new HashMap<Department, String>();
for (Department d : departmentsToDisplay) {
departmentMap.put(d, d.getDepartmentName());
}
result.addObject("departmentMap", departmentMap);
return result;
}
POST method:
#RequestMapping(value = "/addOffice", method = RequestMethod.POST)
public ModelAndView updateOffice(#ModelAttribute("office") Office office, BindingResult result) {
System.out.println("Office Name: " + office.getOfficeName());
System.out.println("Departments: " + office.getDepartments());
return new ModelAndView("result", "command", office);
}
Excerpt from Office.java:
#ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "offices")
private List<Department> departments;
Excerpt from Department.java:
#ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
#JoinTable(name="OFF_DEPT_T",
joinColumns={#JoinColumn(name="DEPT_ID", referencedColumnName="ID")},
inverseJoinColumns={#JoinColumn(name="OFF_ID", referencedColumnName="ID")}
)
private List<Office> offices = new ArrayList<Office>();
If I print the response.getAllErrors() I get:
Field error in object 'office' on field 'departments': rejected value [package.domain.Department#5597e5cf,package.domain.Department#2d14d0a7]; codes [typeMismatch.office.departments,typeMismatch.departments,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [office.departments,departments]; arguments []; default message [departments]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 'departments'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [package.domain.Department] for property 'departments[0]': no matching editors or conversion strategy found]
Can anybody show me how to pass the object but display the String? Thanks.
Problem 1:
As you are missing initialisation of List departments. You should initialise it before putting it in model by replacing:
ModelAndView result = new ModelAndView("officeForm", "command", new Office());
with:
Office office = new Office():
office.setDepartments(new ArrayList<Department>()):
ModelAndView result = new ModelAndView("officeForm", "command", office);
Or if you don't want initialisation in controller, you can initialise it at the time of creation of Office object, like below:
private List<Department> departments = new ArrayList<Department>();
Problem 2:
As you want to bind custom object (Department) list in your select path, you need to provide a custom Property Editor to the data binder, like below:
First create a Property Editor class, something like this:
public class DepartmentEditor extends PropertyEditorSupport {
#Override
public void setAsText(String text) throws IllegalArgumentException {
Department department = new department();
department.setName(text);
setValue(department);
}
}
Then register the property Editor by registering it. You can register it, by simply putting an initBinder method in your controller class like below:
#InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Department.class, new DepartmentEditor());
}
Now, your code should work fine.
I bound command to a new office attribute.
#RequestMapping(value = "/officeSearch", method=RequestMethod.GET)
public String showOfficesSearch(Model model) {
model.addAttribute("command", new Office());
return "officeSearch";
}