java中的接口是类吗
197
2022-09-10
实例详解SpringMVC入门使用
MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model),视图(View)和控制器(Controller).通过分层使开发的软件结构更清晰,从而达到开发效率的提高,可维护性和扩展性得到提高.Spring提供的MVC框架是在J2EE Web开发中对MVC模式的一个实现,本文通过实例讲解一下Spring MVC 的使用.
先来看一个HTTP request在Spring的MVC框架是怎么被处理的:(图片来源于Spring in Action)
1,DispatcherServlet是Spring MVC的核心,它的本质是一个实现了J2EE标准中定义的HttpServlet,通过在web.xml配置
** 以.do结尾的request都会由springTestServlet来处理.
2,3,当接受到request的时候,DispatcherServlet根据HandlerMapping的配置(HandlerMapping的配置文件默认根据
** 以login.do结尾的request由loginController来处理.
4,Controller进行业务处理之后,返回一个ModelAndView对象.
return new ModelAndView(getSuccessView(),"loginDetail",loginDetail);
5,6,DispatcherServlet根据ViewResolver的配置(本例是在springTestServlet-servlet.xml文件中配置)将逻辑view转换到真正要显示的View,如JSP等.
**其作用是将Controller中返回的ModleAndView解析到具体的资源(JSP文件),如上例中的return new ModelAndView(getSuccessView();按照上面ViewResolver配置,会解析成/jsp/loginDetail.jsp.规则为prefix+ModelAndView的第二个参数+suffix.
示例的完整代码如下:
1web.xml:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> prjSpring3
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
prjSpring3
2,springTestServlet-servlet.xml的内容如下:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd">
3,springTest-services.xml的内容:
4,Java代码:
public class LoginController extends SimpleFormController {
org.springframework.web.servlet.DispatcherServlet t;
protected ModelAndView onSubmit(Object command) throws Exception{
LoginCommand loginCommand = (LoginCommand) command;
authenticationService.authenticate(loginCommand);
LoginDetail loginDetail = authenticationService.getLoginDetail(loginCommand.getUserId());
return new ModelAndView(getSuccessView(),"loginDetail",loginDetail);
}
private AuthentihIhhEOcationService authenticationService;
public AuthenticationService getAuthenticationService() {
return authenticationService;
}
public void setAuthenticationService(
AuthenticationService authenticationService) {
this.authenticationService = authenticationService;
}
....
public class LoginCommand {
private String userId;
private String password;
....
public class LoginDetail {
private String userName;
public class AuthenticationService {
public void authenticate(LoginCommand command) throws Exception{
if(command.getUserId()!= null && command.getUserId().equalsIgnoreCase("test")
&& command.getPassword()!= null && command.getPassword().equalsIgnoreCase("test")){
}else{
throw new Exception("User id is not authenticated");
}
}
public LoginDetail getLoginDetail(String userId){
return new LoginDetail(userId);
}
}
5,JSP文件:放在web-inf的jsp文件夹内
login.jsp:
loginDetail.jsp:
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
Login as:
在浏览器的地址栏输入http://localhost:8080/XXX/login.do进入login.jsp页面.
对配置的一些扩充点:
1为HandlerMapping和Controller的配置指定文件名称.
2,加入对MVC注解的支持:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd http://hIhhEOspringframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd"> @Controller public class AnnotationController { @RequestMapping("annotation.do") protected void test(HttpServletRequest request, HttpServletResponse response) throws Exception{ response.getWriter().println("test Spring MVC annotation"); } 3,注解和SimpleFormController同时使用需要在DispatcherServlet对应的servlet配置文件(本例是springTestServlet-servlet.xml)里面配置: 否则会发生下面的异常: javax.servlet.ServletException: No adapter for handler [com.tehIhhEOst.spring.mvc.contoller.LoginController@6ac615]: Does your handler implement a supported interface like Controller? org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:982) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:770) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 在WEB应用中的context配置文件通过下面的方式load,ContextLoaderListener是一个实现了ServletContextListener的listener, 它能够访问 所有可以访问servletContext的地方则可以访问WebApplicationContext. /WEB-INF/springTest-services.xml, classpath:conf/jndiDSAppcontext.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd
http://hIhhEOspringframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd
http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">
@Controller
public class AnnotationController {
@RequestMapping("annotation.do")
protected void test(HttpServletRequest request,
HttpServletResponse response) throws Exception{
response.getWriter().println("test Spring MVC annotation");
}
3,注解和SimpleFormController同时使用需要在DispatcherServlet对应的servlet配置文件(本例是springTestServlet-servlet.xml)里面配置:
否则会发生下面的异常:
javax.servlet.ServletException: No adapter for handler [com.tehIhhEOst.spring.mvc.contoller.LoginController@6ac615]: Does your handler implement a supported interface like Controller?
org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:982)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:770)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
在WEB应用中的context配置文件通过下面的方式load,ContextLoaderListener是一个实现了ServletContextListener的listener,
它能够访问
所有可以访问servletContext的地方则可以访问WebApplicationContext.
/WEB-INF/springTest-services.xml,
classpath:conf/jndiDSAppcontext.xml
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~