spring MVC配置,六步简单搞定

由于单位要求,本人第一次使用spring MVC,一整感觉比ssh要方便简洁很多,如下是我做的一个小实例:
第一步:新建一个web project
第二步:导入spring的包,需要导入spring的core libraries和web libraries
第三步:配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<display-name></display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>testSpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>testSpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第四步:在WEB-INF目录下建立一个testSpringMVC.xml文件,文件名要与web.xml文件中<servlet>
<servlet-name>testSpringMVC</servlet-name> 名称相同
testSpringMVC.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:p="http://www.springframework.org/schema/p"   
        xmlns:context="http://www.springframework.org/schema/context"   
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
<context:annotation-config />
<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="com.test.controller" />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> 
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp"/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8"/>
</beans>

第五步:创建Controller类
package com.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.test.Print;
import com.test.entity.User;
import com.test.service.UserService;

@Controller
@RequestMapping("/user.do")
public class UserController {
@Autowired
private UserService userService;

@RequestMapping
public String queryUser(){
Print.printOut("*******queryUser******************");
userService.getUserList();
return "test";
}
@RequestMapping(params="ac=del")
public String delUser(){
Print.printOut("**********delUser***************");
userService.delUser(new User());
return "del";
}

@RequestMapping(params="ac=up")
public String upUser(){
Print.printOut("*******upUser******************");
userService.upUser(new User());
return "test";
}
}

第六步:创建jsp文件,文件放在testSpringMVC-servlet.xml文件配置的指定目录下,文件名称与UserController类中,方法返回的名称一致。

猜你喜欢

转载自iceelor.iteye.com/blog/1266202
今日推荐