Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7623715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:46:26+00:00 2026-05-31T04:46:26+00:00

I am creating a simple blog web application using play framework, where there are

  • 0

I am creating a simple blog web application using play framework, where there are certain categories and users can post to the category they want, however the problem is that when i post to one category the post is posted to all categories as well.

Application code:
    import java.util.*;
    import models.*;
    public class Application extends Controller {
        public static void index() {
            List Posts = Post.find ("order by type desc").fetch();
            render(Posts); 
        }
        public static void addpost (String content) {
            Post post = new Post(content).save();

            renderJSON(content);
html code:                 
<h2>
        CollabBlog
    </h2>
#{extends 'main.html' /}
#{set title:'Home' /}
<head>
        <title>#{get 'title' /}</title>     
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link rel="stylesheet" type="text/css" media="screen" 
            href="@{'/public/stylesheets/categories.css'}" />        
    </head> 
<h3>Category: Health</h3>
#{ifnot Posts} 
    No posts to view
#{/ifnot}
<ul>
    #{list items:Posts, as: 'Post'}
    <li>        
        <p>${Post.poster}</p><p>${Post.content}</p>        
    </li>
    #{/list}
</ul>
<p>
    <a id="addpost" href="#">add new Health's post</a>    
</p>
<h3>Category: Politics</h3>
#{ifnot Posts} 
    No posts to view
#{/ifnot}
<ul>
    #{list items:Posts, as: 'Post'}
    <li>        
        <p>${Post.poster}:</p><p>${Post.content}</p>        
    </li>
    #{/list}
</ul>
<p>
    <a id="addpost" href="#">add new Politic's post</a>    
</p>
<h3>Category: Entertainment</h3>
#{ifnot Posts} 
    No posts to view
#{/ifnot}
<ul>
    #{list items:Posts, as: 'Post'}
    <li>        
        <p>${Post.poster}</p><p>${Post.content}</p>        
    </li>
    #{/list}
</ul>
<p>
    <a id="addpost" href="#">add new Entertainment's post</a>    
</p>
<body>
    <link href="index.css" rel="stylesheet" type="text/css">
</body>
<script type="text/javascript" charset="utf-8">
$('#addpost').click(function() {
    $.post('@{addpost()}', {content: prompt('post content ?')}, function(Post){
        $('ul').prepend()
    })
    $.post('@{addpost()}', {poster : prompt('which category ?')}, function(Post){
        $('ul').prepend()
    })   
})
</script>                
            }


        }

anyone knows how to solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:46:27+00:00Added an answer on May 31, 2026 at 4:46 am

    After reading your additional comments (and hopefully I haven’t misunderstood), what you could do in your controller is find all unique categories in your type column. Then for each type (as represented by the categories variable in the code snippet below) you could make this call:

    Map<String, List<Post>> map = new HashMap<String, List<Post>>();
    // the categories variable here is the unique types from the Post table
    for (String category : categories) {
        List<Post> posts = Post.find("type = ?", category).fetch();
        map.put(category, posts);
    }
    render(map);
    

    And then in your view, you can go over the keys of the map which will represent all the category names and then you get the value associated with the key and iterate over the posts for that category.

    The above assumes that the user can type in anything they want. If you actually want them to select just either Health, Politics and Entertainment as per your view example in your original post, then maybe you need to model categories in your database. Some options could be:

    First Option

    You could have two entities: (1) Post and (2) Category. The first you have. The second you will need to create. So create a Category entity like this:

    @Entity
    public class Category extends Model {
    
        @OneToMany(mappedBy = "category", targetEntity = Post.class)
        public List<Post> posts;
    
        ...
    }
    

    Then in your Post entity you can have a relationship between the Post and Category entities like this:

    @Entity
    public class Post extends Model {
    
        @ManyToOne
        @JoinColumn(name = "category_id")
        public Category category;
    
        ...
    }
    

    This would be good if a user (or you) could create categories dynamically (or directly in the database) to allow users to select pre-defined categories.

    Second Option

    If the above example is overkill because category is simply a word and doesn’t have other properties like description, then you could just use an enum for category:

    public class Post extends Model {
    
        @Enumerated(EnumType.STRING)
        public Category category;
    
        ...
    }
    

    Where Category is an enum like this:

    public enum Category {
        HEALTH,
        ENTERTAINMENT,
        POLITICS
    }
    

    Again this option means that the user would be selecting from a pre-defined list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating simple blog web application using play framework and i want to
I have a cakePhp application built, and now I'm creating a simple blog based
ROR Sample Application URL - http://sixrevisions.com/web-development/how-to-create-a-blog-from-scratch-using-ruby-on-rails/ I tried above reference for creating a blog
I'm trying to create a simple web-application, which allows users create topics and comment
I am trying to learn MVC and nHibernate by creating a simple blog application.
Creating a simple blog application, I have this partial .comment %p %b Namn: =
I'm creating a simple blog application, and I am having writers block on where
I'm building a simple blog with comments. There is a Post model and a
I am creating a simple blog as part of a website and I am
Was creating a simple console application to do some prototyping and was shocked to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.