Java j2ee spring mvc小型应用程序最佳实践
我正在使用使用spring hibernate的小应用程序,但我是spring MVC领域的新手,我有一些问题: 1) 对多个页面使用单个控制器是一种很好的做法,或者我应该为每个页面创建单独的cont.类 2) 我不想使用spring的表单标签,我使用的是html表单 我的控制器如下:Java j2ee spring mvc小型应用程序最佳实践,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在使用使用spring hibernate的小应用程序,但我是spring MVC领域的新手,我有一些问题: 1) 对多个页面使用单个控制器是一种很好的做法,或者我应该为每个页面创建单独的cont.类 2) 我不想使用spring的表单标签,我使用的是html表单 我的控制器如下: package com.servlets.controllers; import com.utils.generalUtils; import java.io.IOException; import java.
package com.servlets.controllers;
import com.utils.generalUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class signin{
@RequestMapping(value = "/login", method={RequestMethod.POST,RequestMethod.GET})
public ModelAndView loginForm(HttpServletRequest req,HttpServletResponse response){
HashMap<String,String> lsMsg = new HashMap<String,String>();
lsMsg = generalUtils.getInstance().LoginCheck(req);
for (Map.Entry<String, String> entry : lsMsg.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(" key -- "+key+" value -- "+value);
}
if((lsMsg.get("Authorized")).equals("true")){
return new ModelAndView("landing", "message", lsMsg);
}
return new ModelAndView("login", "message", lsMsg);
}
@RequestMapping(value = "/fergot", method={RequestMethod.POST,RequestMethod.GET})
public ModelAndView fergotForm(HttpServletRequest req) { // Not implemented yet
HashMap<String,String> lsMsg = new HashMap<String,String>();
return new ModelAndView("fergot", "message", lsMsg);
}
@RequestMapping("/register")
public ModelAndView registerForm(HttpServletRequest req,HttpServletResponse response) throws IOException{
HashMap<String,String> lsMsgs = new HashMap<String,String>();
lsMsgs.put("Authorized", "false");
lsMsgs = generalUtils.getInstance().addUser(req);
if((lsMsgs.get("Authorized")).equals("true")){
response.sendRedirect("login.html");
}
return new ModelAndView("register", "message", lsMsgs);
}
}
package com.servlets.controllers;
导入com.utils.generalUtils;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.Map;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.servlet.ModelAndView;
@控制器
公共类签名{
@RequestMapping(value=“/login”,method={RequestMethod.POST,RequestMethod.GET})
公共模型和视图登录信息(HttpServletRequest-req,HttpServletResponse-response){
HashMap lsMsg=新HashMap();
lsMsg=generalUtils.getInstance().LoginCheck(req);
对于(Map.Entry:lsMsg.entrySet()){
String key=entry.getKey();
字符串值=entry.getValue();
System.out.println(“键--”+key+“值--”+value);
}
如果((lsMsg.get(“Authorized”)).equals(“true”)){
返回新的ModelAndView(“着陆”、“消息”、lsMsg);
}
返回新的ModelAndView(“登录”、“消息”、lsMsg);
}
@RequestMapping(value=“/fergot”,method={RequestMethod.POST,RequestMethod.GET})
公共模型和视图fergotForm(HttpServletRequest-req){//尚未实现
HashMap lsMsg=新HashMap();
返回新的ModelAndView(“fergot”、“message”、lsMsg);
}
@请求映射(“/寄存器”)
公共模型和视图注册表执行(HttpServletRequest-req,HttpServletResponse-response)抛出IOException{
HashMap lsMsgs=新HashMap();
lsMsgs.put(“授权”、“虚假”);
lsMsgs=generalUtils.getInstance().addUser(req);
如果((lsMsgs.get(“Authorized”)).equals(“true”)){
sendRedirect(“login.html”);
}
返回新的ModelAndView(“注册”、“消息”、lsMsgs);
}
}
1)双向都可以。但您需要最佳实践,所以我的建议是为每个页面创建单独的控制器类。在大多数情况下,这是常见的选择。另一方面,如果你的应用非常非常小,即使你使用单一的控制器也不是什么大问题
选择哪种方式的核心问题是,哪种方式在将来更容易维护,哪种方式对其他人更具可读性
2) 您说您不想使用spring表单标记,当然可以。放轻松,你不能用你不想用的弹簧的任何部分,这是可以的