java web design best practice

herowach 2016-10-13 10:49:56
做个demo的时候遇到几个疑问,想听听大家是怎么做的。技术栈暂定为 前端UI框架+Spring MVC+ pure jdbc/Mybatis;分层为UI + Controller + service + dao

举例:
有一个User表(id, name....)和一个Post(id, userId, subject...)表, 主要实现增删改查:

UserController--->UserService--->UserDao
PostController--->PostService--->PostDao

关于VO:
1. 表单提交的VO和记录显示的VO不一致时,需要创建两个VO吗?还是直接就用Map?(这里的VO主要是Controller里映射后接收/返回的对象)
例如: 提交时只有user的信息,而列表显示时是多表混合的信息,且肯定不会有password等字段。

2. 用户新增的信息被封装到UserVO给到Controller->service,在service 给到 dao的时候,有必要转成一个属性一样的User(model?)/UserDTO(dto?)在传过去吗?
如果不转,当VO与表里字段不一致时怎么办(例如一个VO同时包含几个表的信息)

关于数据合并:
要在列表里同时显示User和post的信息, 应该在哪里合并数据:
1.直接通过sql查出合并后的数据直接返回, 但感觉这样dao就涉及到业务逻辑了;
2.在service层里分别查出User和Post的信息,程序合并成Map/VO返回给Countroller


关于业务异常:
service层遇到业务异常一般应该怎么通知Countroller? 抛出一个自定义的异常(checked or unchecked?) 或者返回一个特定值(例如null)?
举例: findById(id) : 用户没找到,抛异常还是return null
deleteUser(id) : 发现id是管理员,禁止删除,怎么通知Controller
...全文
350 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
herowach 2016-10-14
  • 打赏
  • 举报
回复
CSDN 现在好冷清啊,现在都在哪个上面交流呢? 知乎?
yiguanyagao 2016-10-13
  • 打赏
  • 举报
回复
1 建议(发现id是管理员,禁止删除,怎么通知Controller ),如果是管理员返回一个标记,比如:“admin”,页面删除数据一般用ajax 2 要在列表里同时显示User和post的信息, 应该在哪里合并数据:业务逻辑全部写在service解决,controller只是跳转,传值 3 关于VO: mybities可以接受map,你想用哪些数据直接使用就可以了
yiguanyagao 2016-10-13
  • 打赏
  • 举报
回复
关于业务异常:1,建议(用户没找到,抛异常还是return null),返回null
yiguanyagao 2016-10-13
  • 打赏
  • 举报
回复
我觉得你想的有点复杂,业务异常,这就是你逻辑错误,做项目之前一定将需求了解透
herowach 2016-10-13
  • 打赏
  • 举报
回复
引用 2 楼 yiguanyagao 的回复:
1 建议(发现id是管理员,禁止删除,怎么通知Controller ),如果是管理员返回一个标记,比如:“admin”,页面删除数据一般用ajax 2 要在列表里同时显示User和post的信息, 应该在哪里合并数据:业务逻辑全部写在service解决,controller只是跳转,传值 3 关于VO: mybities可以接受map,你想用哪些数据直接使用就可以了
delete 操作返回值一般都是void,不会返回的,返回boolean也不好,错误信息丢失了;感觉因为业务逻辑失败肯定抛异常,但一般抛checked 还是unchecked异常, 另外就是什么情况下算业务异常,user/post not found这种到底算不算 我知道可以用map,最灵活,既可以当VO也可以当TO,但感觉这样不好,语意没了,并且类可以提供一些简单内聚处理。
java 核心技术 第八版 core 卷1 Core Java, 8th Edition Core Java. Volume I. Fundamentals, 8th Edition Core Java. Volume II. Advanced Features, 8th Edition 官方网站 http://horstmann.com/corejava.html ------------------------------------------------ Core Java. Volume I. Fundamentals, 8th Edition ------------------------------------------------ Author: Cay S. Horstmann, Gary Cornell Publisher: Prentice Hall PTR Number Of Pages: 864 Publication Date: 2007-09-11 Sales Rank: 15617 ISBN-10/ASIN: 0132354764 ISBN-13/EAN: 9780132354769 Binding: Paperback Manufacturer: Prentice Hall PTR Studio: Prentice Hall PTR Average Rating: 5 This revised edition of the classic Core Java™, Volume I–Fundamentals, is the definitive guide to Java for serious programmers who want to put Java to work on real projects. Fully updated for the new Java SE 6 platform, this no-nonsense tutorial and reliable reference illuminates the most important language and library features with thoroughly tested real-world examples. The example programs have been carefully crafted to be easy to understand as well as useful in practice, so you can rely on them as an outstanding starting point for your own code. Volume I is designed to quickly bring you up to speed on what’s new in Java SE 6 and to help you make the transition as efficiently as possible, whether you’re upgrading from an earlier version of Java or migrating from another language. The authors concentrate on the fundamental concepts of the Java language, along with the basics of user-interface programming. You’ll find detailed, insightful coverage of Java fundamentals Object-oriented programming Interfaces and inner classes Reflection and proxies The event listener model GUI programming with Swing Packaging applications Exception handling Logging and debugging Generic programming The collections framework Concurrency For detailed coverage of XML processing, networking, databases, internationalization, security, advanced AWT/Swing, and other advanced features, look for the forthcoming eighth edition of Core Java™, Volume II—Advanced Features (ISBN: 978-0-13-235479-0). Review: Latest edition of an old favorite updated for Java 6 This is the latest edition and eighth round of the "Core Java" series of books. When it first came out in 1996 it had no competitors, and now alternatives abound, in particular the "Head First" series for learning Java published by O'Reilly and Associates. However, this is still my recommendation both for people who are learning Java and for those who are old pros and just want to catch up on what's new in the language. There are plenty of code examples, and the explanations are very clear as in the previous editions. This series always comes in two volumes - the fundamentals volume appears first followed by an "advanced features" volume a few months later. Both are usually worthwhile due to their comprehensive coverage of the language. The following is the table of contents for the eighth edition, not yet shown on the product page at the time I am writing this: Chapter 1: An Introduction to Java 1 Java As a Programming Platform 2 The Java "White Paper" Buzzwords 2 Java Applets and the Internet 7 A Short History of Java 9 Common Misconceptions about Java 11 Chapter 2: The Java Programming Environment 15 Installing the Java Development Kit 16 Choosing a Development Environment 21 Using the Command-Line Tools 22 Using an Integrated Development Environment 25 Running a Graphical Application 28 Building and Running Applets 31 Chapter 3: Fundamental Programming Structures in Java 35 A Simple Java Program 36 Comments 39 Data Types 40 Variables 44 Operators 46 Strings 53 Input and Output 63 Control Flow 71 Big Numbers 88 Arrays 90 Chapter 4: Objects and Classes 105 Introduction to Object-Oriented Programming 106 Using Predefined Classes 111 Defining Your Own Classes 122 Static Fields and Methods 132 Method Parameters 138 Object Construction 144 Packages 15 The Class Path 160 Documentation Comments 162 Class Design Hints 167 Chapter 5: Inheritance 171 Classes, Superclasses, and Subclasses 172 Object: The Cosmic Superclass 192 Generic Array Lists 204 Object Wrappers and Autoboxing 211 Methods with a Variable Number of Parameters 214 Enumeration Classes 215 Reflection 217 Design Hints for Inheritance 238 Chapter 6: Interfaces and Inner Classes 241 Interfaces 242 Object Cloning 249 Interfaces and Callbacks 255 Inner Classes 258 Proxies 275 Chapter 7: Graphics Programming 281 Introducing Swing 282 Creating a Frame 285 Positioning a Frame 288 Displaying Information in a Component 294 Working with 2D Shapes 299 Using Color 307 Using Special Fonts for Text 310 Displaying Images 318 Chapter 8: Event Handling 323 Basics of Event Handling 324 Actions 342 Mouse Events 349 The AWT Event Hierarchy 357 Chapter 9: User Interface Components with Swing 361 Swing and the Model-View-Controller Design Pattern 362 Introduction to Layout Management 368 Text Input 377 Choice Components 385 Menus 406 Sophisticated Layout Management 424 Dialog Boxes 452 Chapter 10: Deploying Applications and Applets 493 JAR Files 494 Java Web Start 501 Applets 516 Storage of Application Preferences 539 Chapter 11: Exceptions, Logging, Assertions, and Debugging 551 Dealing with Errors 552 Catching Exceptions 559 Tips for Using Exceptions 568 Using Assertions 571 Logging 575 Debugging Tips 591 Using a Debugger 607 Chapter 12: Generic Programming 613 Why Generic Programming? 614 Definition of a Simple Generic Class 616 Generic Methods 618 Bounds for Type Variables 619 Generic Code and the Virtual Machine 621 Restrictions and Limitations 626 Inheritance Rules for Generic Types 630 Wildcard Types 632 Reflection and Generics 640 Chapter 13: Collections 649 Collection Interfaces 650 Concrete Collections 658 The Collections Framework 689 Algorithms 700 Legacy Collections 707 Chapter 14: Multithreading 715 What Are Threads? 716 Interrupting Threads 728 Thread States 730 Thread Properties 733 Synchronization 736 Blocking Queues 764 Thread-Safe Collections 771 Callables and Futures 774 Executors 778 Synchronizers 785 Threads and Swing 794 ------------------------------------------------------ Core Java. Volume II. Advanced Features, 8th Edition ------------------------------------------------------ Author: Cay S. Horstmann, Gary Cornell Publisher: Prentice Hall PTR Pages: 1056 Publication Date: 2008-04-19 ISBN-10/ASIN: 0132354799 ISBN-13/EAN: 9780132354790 Binding: Paperback The revised edition of the classic Core Java. Volume II–Advanced Features, covers advanced user-interface programming and the enterprise features of the Java SE 6 platform. Like Volume I (which covers the core language and library features), this volume has been updated for Java SE 6 and new coverage is highlighted throughout. All sample programs have been carefully crafted to illustrate the latest programming techniques, displaying best-practices solutions to the types of real-world problems professional developers encounter. Volume II includes new sections on the StAX API, JDBC 4, compiler API, scripting framework, splash screen and tray APIs, and many other Java SE 6 enhancements. In this book, the authors focus on the more advanced features of the Java language, including complete coverage of Streams and Files Networking Database programming XML JNDI and LDAP Internationalization Advanced GUI components Java 2D and advanced AWT JavaBeans Security RMI and Web services Collections Annotations Native methods For thorough coverage of Java fundamentals–including interfaces and inner classes, GUI programming with Swing, exception handling, generics, collections, and concurrency–look for the eighth edition of Core Java. Volume I–Fundamentals (ISBN: 978-0-13-235476-9).
Key Features Learn about Enterprise Architects IT strategy and NFR – this book provides you with methodologies, best practices, and frameworks to ace your interview A holistic view of key architectural skills and competencies with 500+ questions that cover 12 domains 100+ diagrams depicting scenarios, models, and methodologies designed to help you prepare for your interview Book Description An architect attends multiple interviews for jobs or projects during the course of his or her career. This book is an interview resource created for designers, consultants, technical, solution, domain, enterprise, and chief architects to help them perform well in interview discussions and launch a successful career. The book begins by providing descriptions of architecture skills and competencies that cover the 12 key domains, including 350+ questions relating to these domains. The goal of this book is to cover all the core architectural domains. From an architect’s perspective, it is impossible to revise or learn about all these key areas without a good reference guide – this book is the solution. It shares experiences, learning, insights, and proven methodologies that will benefit practitioners, SMEs, and aspirants in the long run. This book will help you tackle the NFR domain, which is a key aspect pertaining to architecting applications. It typically takes years to understand the core concepts, fundamentals, patterns, and principles related to architecture and designs. This book is a goldmine for the typical questions asked during an interview and will help prepare you for success! What you will learn Learn about IT strategies, NFR, methodologies, best practices, and frameworks to ace your interview Get a holistic view of key concepts, design principles, and patterns related to evangelizing web and Java enterprise applications Discover interview preparation guidelines through case studies Use this as a reference guide for adopting best practices, standards, and design guidelines Get a better understanding with 60+ diagrams depicting various scenarios, models, and methodologies Benefit from coverage of all architecture domains including EA (Business, Data, Infrastructure, and Application), SA, integration, NFRs, security, and SOA, with extended coverage from IT strategies to the NFR domain About the Author Sameer Paradkar is an enterprise architect with 15+ years of solid experience in the ICT industry which spans across consulting, systems integration, and product development. He is an Open Group TOGAF, Oracle Master Java EA, TMForum NGOSS, IBM SOA Solutions, IBM Cloud Solutions, IBM MobileFirst, ITIL Foundation V3 and COBIT 5 certified enterprise architect. He serves as an advisory architect on enterprise architecture programs and continues to work as a subject matter expert. He has worked on multiple architecture transformations and modernization engagements in the USA, UK, Europe, Asia Pacific and the Middle East Regions that presented a phased roadmap to the transformation that maximized the business value while minimizing risks and costs. Sameer is part of IT Strategy and Transformation Practice in AtoS. Prior to AtoS, he has worked in organizations such as EY - IT Advisory, IBM GBS, Wipro Consulting Services, TechMahindra, and Infosys Technologies and specializes in IT strategies and enterprise transformation engagements. Table of Contents Chapter 1. Architect Roles and Growth Paths Chapter 2. IT Strategy and Advisory Chapter 3. Enterprise Architecture and Modernization Chapter 4. SOA and Integration Chapter 5. Solution Architecture and Design Chapter 6. Emerging Technologies Chapter 7. Methodologies, Frameworks, and NFRs Chapter 8. Interview Preparation
Cracking the IT Architect Interview English | 30 Nov. 2016 | ISBN: 1787121690 | 372 Pages | AZW3/MOBI/EPUB/PDF (conv) | 22.09 MB Key Features Learn about Enterprise Architects IT strategy and NFR – this book provides you with methodologies, best practices, and frameworks to ace your interview A holistic view of key architectural skills and competencies with 500+ questions that cover 12 domains 100+ diagrams depicting scenarios, models, and methodologies designed to help you prepare for your interview Book Description An architect attends multiple interviews for jobs or projects during the course of his or her career. This book is an interview resource created for designers, consultants, technical, solution, domain, enterprise, and chief architects to help them perform well in interview discussions and launch a successful career. The book begins by providing descriptions of architecture skills and competencies that cover the 12 key domains, including 350+ questions relating to these domains. The goal of this book is to cover all the core architectural domains. From an architect’s perspective, it is impossible to revise or learn about all these key areas without a good reference guide – this book is the solution. It shares experiences, learning, insights, and proven methodologies that will benefit practitioners, SMEs, and aspirants in the long run. This book will help you tackle the NFR domain, which is a key aspect pertaining to architecting applications. It typically takes years to understand the core concepts, fundamentals, patterns, and principles related to architecture and designs. This book is a goldmine for the typical questions asked during an interview and will help prepare you for success! What you will learn Learn about IT strategies, NFR, methodologies, best practices, and frameworks to ace your interview Get a holistic view of key concepts, design principles, and patterns related to evangelizing web and Java enterprise applications Discover inte

81,117

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧