SpringMVC的简单示例
首先导入所需的jar包,项目目录结构如下:
之后需要配置一下web.xml文件,内容如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 3 <servlet> 4 <servlet-name>springDispatcherServlet</servlet-name> 5 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 6 <init-param> 7 <param-name>contextConfigLocation</param-name> 8 <param-value>classpath:applicationContext.xml</param-value> 9 </init-param> 10 <load-on-startup>1</load-on-startup> 11 </servlet> 12 <servlet-mapping> 13 <servlet-name>springDispatcherServlet</servlet-name> 14 <url-pattern>/</url-pattern> 15 </servlet-mapping> 16 </web-app>
然后配置applicationContext.xml:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:context="http://www.springframework.org/schema/context" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 9 10 <!-- 配置自动扫描的包 --> 11 <context:component-scan base-package="com.sprmvc.po"></context:component-scan> 12 <!-- 配置视图解析器 将HelloWorldController中的返回值解析为实际的物理视图 --> 13 <bean 14 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 15 <!-- prefix为前缀,suffix为后缀 --> 16 <property name="prefix" value="/"></property> 17 <property name="suffix" value=".jsp"></property> 18 </bean> 19 </beans>
下面开始建立实体类User.java:
1 package com.sprmvc.po; 2 3 public class User { 4 private String userName; 5 private String password; 6 public String getUserName() { 7 return userName; 8 } 9 public void setUserName(String userName) { 10 this.userName = userName; 11 } 12 public String getPassword() { 13 return password; 14 } 15 public void setPassword(String password) { 16 this.password = password; 17 } 18 }
然后是控制层代码:
1 package com.sprmvc.po; 2 3 import javax.servlet.http.HttpServletRequest; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestMethod; 8 9 @Controller 10 public class HelloWorldController { 11 @RequestMapping(value="/hello",method = RequestMethod.GET) 12 public String printHelloWorld(HttpServletRequest request,User user) { 13 request.setAttribute("userName", user.getUserName()); 14 request.setAttribute("password", user.getPassword()); 15 return "hello"; 16 } 17 }
这里的value即访问路径,而return的"hello"通过applicationContext.xml中配置的视图解析器会返回到hello.jsp中
接下来我们建立两个.jsp页面,首先是index.jsp:
1 <body> 2 <form method="get" action="hello"> 3 用户名:<input type="text" name="userName"> 4 密码:<input type="password" name="password"><br> 5 <input type="submit" value="提交"> 6 </form> 7 </body>
这里的method和action分别与控制层中的method和value的值相对应,即HelloWorldController.java中的第11行。
然后是hello.jsp:
1 <body> 2 <h1>操作成功了</h1> 3 您的用户名为:${userName}<br> 4 您的密码为:${password } 5 </body>
这样只需要将项目加载到tomcat下就可以进行访问了,赶快试试把。