我正在设计一个简单的基于Web的应用程序.我是这个基于Web的域的新手.我需要您对设计模式的建议,例如如何在Servlet中分配责任,制作新Servlet的标准等.
实际上,我的主页上有很少的实体,并且每个实体都对应于我们添加,编辑和删除等几个选项.之前我每个选项使用一个Servlet,比如Servlet1,用于添加entity1,Servlet2用于编辑entity1等等,这样我们最终得到了大量的servlet.
现在我们正在改变我们的设计.我的问题是你如何选择如何选择servlet的责任.我们是否应该为每个实体安装一个Servlet,它将处理所有选项并将请求转发给服务层.或者我们应该为整个页面都有一个servlet来处理整个页面请求,然后将其转发到相应的服务层?此外,请求对象是否应转发到服务层.
我认为解决此问题的最佳方法是粘贴我的代码:
Selector bean
@ManagedBean(name="selector")
@RequestScoped
public class Selector {
@ManagedProperty(value="#{param.page}")
private String page;
private String profilePage;
@PostConstruct
public void init() {
if(profilePage==null || profilePage.trim().isEmpty()) {
this.profilePage="main";
}
if(page==null || page.trim().isEmpty()) {
this.page="homepage";
}
}
public String getProfilePage() { System.out.println("GET ="+profilePage); return profilePage; }
public void setProfilePage(String profilePage) { this.profilePage=profilePage; }
public String getPage() { return page; }
public void setPage(String page) { this.page=page; }
}
Run Code Online (Sandbox Code Playgroud)
profile.xhtml
<h:panelGroup layout="block" id="profileContent">
<h:panelGroup rendered="#{selector.profilePage=='main'}">
<ui:include src="/profile/profile_main.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{selector.profilePage=='edit'}">
<ui:include src="/profile/profile_edit.xhtml" /> …
Run Code Online (Sandbox Code Playgroud)