JSP in Javascript file or multiple AJAX requests

I’ve been thinking about how I want to load my data for some checkboxes in an AngularJS app. I’m generating multiple checkboxes based on 2 JSON objects that are currenty hard-coded in the a Javascript file (it’s currently a prototype, but in production the values for the checkboxes can change from day to day).

Now I’ve tried using JSP in my Javascript file (IDE is Eclipse) – while it would work, it just looks ugly (besides the fact that JSP syntax checking doesn’t work in a .js file). I thought about creating a servlet that would generate those JSON objects and then loading them through AJAX, but I’m not sure if this might be “bad behaviour” since I’d generate multiple AJAX calls only to keep my code “clean”.

Is there some kind of convention or best-practice for dynamically created Javascript or should I usually prefer AJAX over JSP in Javascript?

What you want to do is possible and there are available in many frameworks that can help here. Look at AJAX as a callback implemented in JS and not something different – this will help simplify your problem. There is no best practice IMO, but there are some options available that I have used in the past. Here are some options,

  1. JSP Tag UI Components: You have some widget which requires some business input to render but is mostly JavaScript. While AJAX is an option it can be unnecessary if the data is available at the point of rendering the JSP. One approach is to create a JSP Tag Lib that internally renders the HTML and JS. The benefit is fewer people are touching and working with JS which is more error prone and relatively difficult to Unit Test. In the past this was a messy option since you would have to use multiple out.println statements to render the Dynamic JS from the Tag Handler but now with the introduction of Tag Files this can be a lot cleaner.

  2. JS Endpoint: JS Endpoints are special servlets that serve dynamic JS. Think of this instead you setting a static file in the src attribute of the script tag you have the URL pointing to a URL pointing to a Servlet that responds with content type for JavaScript or JSON as the case may be.

    <script src="/myapp/abc.jsx"></script>
    

    In the web.xml,

    <servlet-mapping>
    <servlet-name>myServletThatServesDynamicJS</servlet-name>
    <url-pattern>*.jsx</url-pattern>
    </servlet-mapping>
    
  3. Client-side Frameworks: There are many JS frameworks worth your time investment that can minimize the amount of JS you write by declaratively generate a lot of JS (and CSS) something that most Java developers would prefer. AngularJS and Bootstrap are examples. So you basically add some additional attributes to regular HTML tags and there is some magic in the background that generates components, makes AJAX calls, etc. Angular will pull required JS “modules” from the server as an when needed via AJAX calls and also supports advance features like DI which can make testing your JS code a breeze and also a lot cleaner.

I personally prefer using JS frameworks to solve JS problems. So I would be in favor of option 3 for most of the issues. JS frameworks have come a long way and have wide industry support especially when you are talking of Frameworks like AngularJS or even jQuery. They also address many other concerns some that you haven’t raised like cross browser compatibility which can be a huge pain even if you are supporting just one browser across versions.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật