Spring MVC works with web designers

When you work with web designers in Spring-MVC and JSP environments, are there any tools or useful templates to reduce the pain of moving from HTML to JSP and vice versa?

Project requirements dictate that views change frequently, and it can be difficult to make changes efficiently due to the amount of Java code that flows into the view layer. Ideally, I would like to remove almost all of the Java code from the view, but it doesn't seem to work with the Spring / JSP philosophy, where often the way to remove Java code is to replace that code with tag libraries, which will still have a similar problem.

To give a little clarity in my question, I'm going to include some existing code (inherited by me) to show the problems that I will probably encounter when changing the look of our views:

<%-- Begin looping through results --%>
<%
List memberList = memberSearchResults.getResults();
for(int i = start - 1; i < memberList.size() && i < end; i++) {
    Profile profile = (Profile)memberList.get(i);                  
    long profileId = profile.getProfileId();
    String nickname = profile.getNickname();
    String description = profile.getDescription();
    Image image = profile.getAvatarImage();
    String avatarImageSrc = null;
    int avatarImageWidthNum = 0;
    int avatarImageHeightNum = 0;

    if(null != image) {
        avatarImageSrc = image.getSrc();
        avatarImageWidthNum = image.getWidth();
        avatarImageHeightNum = image.getHeight();
    }

String bgColor = i % 2 == 1 ? "background-color:#FFF" : "";
%>
<div style="float:left;clear:both;padding:5px 0 5px 5px;cursor:pointer;<%= bgColor %>" onclick='window.location="profile.sp?profileId=<%= profileId %>"'>
    <div style="float:right;clear:right;padding-left:10px;width:515px;font-size:10px;color:#7e7e7e">
        <h6><%= nickname %></h6>
        <%= description %>
    </div> 
    <img style="float:left;clear:left;" alt="Avatar Image" src="<%= null != avatarImageSrc && avatarImageSrc.trim().length() > 0 ? avatarImageSrc : "images/defaultUserImage.png" %>" 
         <%= avatarImageWidthNum < avatarImageHeightNum ? "height='59'" : "width='92'" %> />
</div>
<%
} // End loop
%>

Now, ignoring some of the code smells, it’s obvious that if someone wants to change the look of this DIV, it will be necessary to move all the Java / JSP code to the new HTML code (designers hope t work in JSP files, they have their own HTML website version). It is tiring and error prone.

+3
source share
1 answer

MVC , JavaServer Pages Taglib JSTL. Spring JSP, :

@Controller
public StuffController
{
    @RequestMapping("bla")
    public ModelAndView doBla()
    {
        ModelAndView view = new ModelAndView();
        // Get memberSearchResults somehow
        MemberSearchResult results = memberSearchResults.getResults();
        view.addAtrribute("memberList", results);
        view.setViewName("blaview");
        return view;
    }
}

JSP,

// blaview.jsp
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>
<c:forEach var="searchResult" items="${memberList}">
    <div>
        <h6>${searchResult.nickname}</h6>
        ${searchResult.description}
    </div> 
</c:forEach>

bean, , JSP. , , , taglib. - taglib. , , - , taglibs - , HTML, Java Source JSP.

+2

Source: https://habr.com/ru/post/1740026/


All Articles