spring MVC一个简单的demo

 以下是本人对spring MVC的学习,如果有不正确的地方,请各位多多指教。

不多啰嗦了,直接来代码示例。

环境: tomcat 7.0, jdk 1.6.0_25, spring 3.2.0

  1. 首先,我一般会先创建一个tomcat工程,这个是根据个人喜好来做。
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    			xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    			version="3.0" metadata-complete="true">
    			
    	<welcome-file-list>
    		<welcome-file>index.htm</welcome-file>
    	</welcome-file-list>
    	
    </web-app>
  2. 创建index.htm
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<h1>test!!!!</h1>
    </body>
    </html>
  3. 配置tomcat的server.xml
    <Context path="/springMVC" docBase="你自己的工程路径" debug="0" reloadable="true">
    </Context>
  4. 测试 打开ie,输入http://localhost:8080/springMVC/ ,出现下面画面,证明请求成功。
    spring MVC一个简单的demo
  5. 编辑web.xml,加入servlet
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    			xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    			version="3.0" metadata-complete="true">
    			
    	<servlet>
    		<servlet-name>springMVC</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	
    	<servlet-mapping>
    		<servlet-name>springMVC</servlet-name>
    		<url-pattern>*.html</url-pattern>
    	</servlet-mapping>
    	
    	<welcome-file-list>
    		<welcome-file>index.htm</welcome-file>
    	</welcome-file-list>
    	
    </web-app>
  6. 配置结束后,这里面有个契约,上面的servlet的名字是springMVC,这样会默认在工程的WEB-INF文件夹下面找springMVC-servlet.xml文件,所以我们要创建springMVC-servlet.xml。当然也自己指定的文件,网上有很清晰的说明,这里就不再说了,如果有不清楚地,也可以留言给我。
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    		xmlns:context="http://www.springframework.org/schema/context"
    		xmlns:util="http://www.springframework.org/schema/util"
    		xsi:schemaLocation="http://www.springframework.org/schema/beans
    							http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    							http://www.springframework.org/schema/context
    							http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    							http://www.springframework.org/schema/util
    							http://www.springframework.org/schema/util/spring-util-2.0.xsd ">
    							
    	<!-- annotation class's package -->
    	<context:component-scan base-package="app"/>
    	
    	<!-- complete request and annotation mapping -->
    	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    	
    	<!-- analyze view path -->
    	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/WEB-INF/jsp/"/>
    		<property name="suffix" value=".jsp"/>
    		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
    	</bean>
    
    </beans>
    
    
     
  7. 以上配置基本就结束了。现在我们开始创建action和form,来测试下。
    package app.login.action;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import app.login.form.LoginForm;
    
    @Controller
    public class LoginAction {
    
    	@RequestMapping("/index")
    	public String index() {
    		return "login/login";
    	}
    
    	@RequestMapping("/login")
    	public ModelAndView login(LoginForm form) {
    		ModelAndView mav = new ModelAndView();
    		mav.addObject("form", form);
    		mav.setViewName("login/login_success_test");
    		return mav;
    	}
    }
  8. package app.login.form;
    
    public class LoginForm {
    
    	private String loginID;
    
    	private String password;
    
    	public String getLoginID() {
    		return loginID;
    	}
    
    	public void setLoginID(String loginID) {
    		this.loginID = loginID;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    }
  9.  创建结束后,我们修改index.htm
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="refresh" content="0;url=index.html">
    <title>Insert title here</title>
    </head>
    </html>
  10. 创建login.jsp
    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    <script type="text/javascript">
    	function doSubmit() {
    		document.getElementById("form").action = "login.html";
    		document.getElementById("form").submit();
    	}
    </script>
    </head>
    <body>
    <form id="form" method="post">
    	loginID:<input type="text" name="loginID" id="loginID" size="10"><BR>
    	password:<input type="password" name="password" id="password" size="10"><BR>
    	<input type="button" value="login" onclick="doSubmit();">
    </form>
    </body>
    </html>
  11. 创建login_success_test.jsp
    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form>
    	<h1>Hello, ${form.loginID}, welcome to spring MVC.</h1>
    </form>
    </body>
    </html>
  12. 测试 打开ie,再次输入http://localhost:8080/springMVC/
    spring MVC一个简单的demo
  13. 输入测试内容
    spring MVC一个简单的demo
     
  14. 跳转到欢迎画面,这样一个简单的demo就完成了
    spring MVC一个简单的demo

你可能感兴趣的:(spring,mvc)