springMvc入门Demo例子

--2013年5月28日13:27:42

园子里不少springMvc入门的Demo,但是不完整或者找着做不出来,我把我的例子总结出来。

理论知识点不讨论,直接说步骤:

1、myeclipse创建helloS的javaWeb项目

2、导入spring的依赖jar包,Myeclipse的library里面的spring 3.0CoreLibraries,Spring 3.0 web libraries

导入后的jar包图:

3、创建一下几个文件

3.1web.xml

3.2springmvc-servlet.xml

3.3springmvc-view.xml

3.4index.jsp

3.5userlist.jsp

​3.6UserController.java 

3.7User.java

详细文件

3.1web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <web-app version="2.5" 
 4     xmlns="http://java.sun.com/xml/ns/javaee"
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 7     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 8         <!--配置springDispatcherServlet的处理类  -->
 9     <servlet>
10         <servlet-name>demoServlet</servlet-name>
11         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
12         <!--加载处理类dispatcher的配置文件 多个用,号隔开 -->
13         <init-param>
14              <param-name>contextConfigLocation</param-name>
15              <param-value>
16                  /WEB-INF/springmvc-servlet.xml,/WEB-INF/springmvc-view.xml    
17              </param-value> 
18         </init-param>
19     </servlet>
20     <!--DispatcherServlet拦截规则  -->
21     <servlet-mapping>
22         <servlet-name>demoServlet</servlet-name>
23         <url-pattern>*.do</url-pattern>
24     </servlet-mapping>
25 </web-app>
view code

3.2springmvc-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <beans xmlns="http://www.springframework.org/schema/beans"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 4     xmlns:tx="http://www.springframework.org/schema/tx"  
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans    
 6     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
 7     http://www.springframework.org/schema/tx      
 8     http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
 9        
10     <!-- 【配置映射处理器】 -->  
11     <!-- SpringMVC自带的映射处理器均实现了Spring的Ordered接口,这意味着我们可以在应用系统中声明多个处理器映射 -->  
12     <!-- 并且可以设置它们的优先级,这主要体现在order属性上,其值越小,则对应的该映射处理器的优先级越高 -->  
13     <!-- 本例中,SimpleUrlHandlerMapping的order值比BeanNameUrlHandlerMapping的小 -->  
14     <!-- 这意味着DispatcherServlet在映射URL的时候,首先会咨询SimpleUrlHandlerMapping -->  
15     <!-- 只有在SimpleUrlHandlerMapping无法返回结果时,才咨询BeanNameUrlHandlerMapping -->  
16     <bean id="beanNameUrlMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">  
17         <property name="order" value="1"/>  
18     </bean>  
19     <bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
20         <property name="order" value="0"/>  
21         <property name="mappings">  
22             <props>  
23                 <prop key="/userlist.do">userController</prop>  
24                 <prop key="/*.do">userController</prop>  
25             </props>  
26         </property>  
27     </bean>  
28   
29     <!-- 这里的id="userController"对应的是<bean id="simpleUrlMapping">中的<prop>里面的value -->  
30     <bean id="userController" class="com.study.controller.UserController"/>  
31        
32     <!-- DispatcherServlet使用的默认处理器映射是BeanNameUrlHandlerMapping,它使用URL样式的名字 -->  
33     <bean name="/userlist.do" class="com.study.controller.UserController"/>  
34 </beans>  
View Code

3.3springmvc-view.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:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
    http://www.springframework.org/schema/tx      
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
       
    <!-- 【配置视图解析器】 -->  
    <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->  
    <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->  
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/"/>  
        <property name="suffix" value=".jsp"/>  
    </bean>  
</beans>  
View Code

 

3.4index.jsp

<%@ page language="java" pageEncoding="UTF-8"%>  
<a href="<%=request.getContextPath()%>/userlist11.do">Visit "/userlist11.htm" to my SpringMVC demo page</a>  
<br/>  
<br/>  
<a href="<%=request.getContextPath()%>/userlist22.do">Visit "/userlist22.htm" to my SpringMVC demo page</a>  
<br/>  
<br/>  
<a href="<%=request.getContextPath()%>/userlist33.do">Visit "/userlist33.htm" to my SpringMVC demo page</a>  
View Code

 

3.5userlist.jsp

<%@ page language="java" pageEncoding="UTF-8"%>  
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
<h2>Welcome to my SpringMVC demo page</h2>  
<c:forEach items="${users}" var="user">  
    <c:out value="${user.username}"/><br/>  
    <c:out value="${user.password}"/><br/>  
    <c:out value="${user.address}"/><br/>  
    <c:out value="${user.age}"/><br/>  
</c:forEach>  
  
<!--   
以下是,处理对"/userlist.htm"的请求的整个工作步骤   
1、DispatcherServlet接受对"/userlist.htm"样式的URL请求   
2、DispatcherServlet询问BeanNameUrlHandlerMapping   
  询问Bean的名字是"/userlist.htm"的控制器,并且找到了UserController的Bean   
3、DispatcherServlet分发给UserController来处理这个请求   
4、UserController返回一个ModelAndView对象,它带有一个逻辑视图名"userlist",以及保存在"users"属性中的值   
5、DispatcherServlet询问它的视图解析器(配置为InternalResourceViewResolver)   
  查找逻辑名为userlist的视图,InternalResourceViewResolver返回/WEB-INF/jsp/userlist.jsp路径   
6、DispatcherServlet将请求导向/WEB-INF/jsp/userlist.jsp页面   
-->  
View Code

​3.6UserController.java 

package com.study.controller;   
  
import java.util.ArrayList;   
import java.util.List;   
  
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;   
  
import org.springframework.web.servlet.ModelAndView;   
import org.springframework.web.servlet.mvc.AbstractController;   
  
import com.study.model.User;
  
 
public class UserController extends AbstractController {   
    @Override  
    protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {   
        List<User> userList = new ArrayList<User>();   
        User user11 = new User();   
        User user22 = new User();   
        user11.setUsername("张三");   
        user11.setPassword("222222");   
        user11.setAddress("dkdkkdk");   
        user11.setAge(27);   
        user22.setUsername("李四");   
        user22.setPassword("95000220");   
        user22.setAddress("王五");   
        user22.setAge(37);   
        userList.add(user11);   
        userList.add(user22);   
        //ModelAndView类在SpringMVC中是一个很重要的概念   
        //控制器执行方法都必须返回一个ModelAndView,ModelAndView对象保存了视图以及视图显示的模型数据   
        //第一个参数:视图组件的逻辑名称。这里视图的逻辑名称是userlist,视图解析器会使用该名称查找实际的View对象   
        //第二个参数:传递给视图的,模型对象的名称   
        //第三个参数:传递给视图的,模型对象的值   
        System.out.println("run here ----------------");
        return new ModelAndView("userlist", "users", userList);   
    }   
}  
View Code

 

3.7User.java

 1 package com.study.model;   
 2   
 3  
 4 public class User {   
 5     private String username;   
 6     private String password;   
 7     private String address;   
 8     private Integer age;   
 9        
10     public String getUsername() {   
11         return username;   
12     }   
13     public void setUsername(String username) {   
14         this.username = username;   
15     }   
16     public String getPassword() {   
17         return password;   
18     }   
19     public void setPassword(String password) {   
20         this.password = password;   
21     }   
22     public String getAddress() {   
23         return address;   
24     }   
25     public void setAddress(String address) {   
26         this.address = address;   
27     }   
28     public Integer getAge() {   
29         return age;   
30     }   
31     public void setAge(Integer age) {   
32         this.age = age;   
33     }   
34 }  
View Code

 

4、部署运行

5、访问:http://localhost:2013/helloS/userlist22.do

应该可以了,步骤说完了。理论的东西以后总结。

----

参考资料:

1、http://www.ishang123.com/jishubowen/java/2012-09-29/252.html

2、http://javacrazyer.iteye.com/blog/790834(这篇文章有错)

posted @ 2013-05-28 13:58  夏远  Views(953)  Comments(0Edit  收藏  举报
Hit Counter
Americas Department Store