Spring MVC配置步骤详解:
一:Spring 各类包的导入。
二:Spring的配置文件:web.xml;applicationContext.xml;dispatcher-servlet.xml;
1、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>sysUserSpringTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 以上为固定格式直接拷过来使用-->
<!-- 指定Spring Bean的配置文件的位置 -->
<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>
<!--配置 Spring MVC -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value> <!-- dispatcher-servlet.xml视具体位置而定,一般放在/WEB-INF/中 -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--把请求交由* .do处理 -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2、applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 启用注解 -->
<context:annotation-config/>
<!-- 扫描基础包 -->
<!-- 包名除了conreoller不需要放在这里,其他的包 如果需要用到的话都需要放到这里,注入方式用的是注解注入-->
<context:component-scan base-package="包名"></context:component-scan>
<context:component-scan base-package="包名"></context:component-scan>
............
<context:component-scan base-package="包名"></context:component-scan>
</beans>
3、dispatcher-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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"
default-autowire="byName">
<!-- 启用注解 -->
<context:annotation-config />
<!-- 扫描包 -->
<context:component-scan base-package="com.controller"></context:component-scan>
<!-- 完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 请求/处理的映射关系 -->
<!-- id与controller中的@Controller的名字保持一致 class的包就是controller所在的包 -->
<bean id="sysUserController" class="com.controller.SysUserController"
p:successView="list.jsp" p:error="login.jsp" p:editSuccess="detail.jsp">
</bean>
</beans>
三、controller代码
package com.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.dao.SysUserDao;
import com.entity.Content;
import com.entity.SysUser;
@Controller(“sysUserController”)
public class SysUserController {
private String successView;
private String error;
private HttpSession session;
private String editSuccess;
public String getEditSuccess() {
return editSuccess;
}
@Autowired
public void setEditSuccess(String editSuccess) {
this.editSuccess = editSuccess;
}
public String getSuccessView() {
return successView;
}
public void setSuccessView(String successView) {
this.successView = successView;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public HttpSession getSession() {
return session;
}
@Autowired
public void setSession(HttpSession session) {
this.session = session;
}
@Autowired
private SysUserDao sysUserDao;
public SysUserDao getSysUserDao() {
return sysUserDao;
}
public void setSysUserDao(SysUserDao sysUserDao) {
this.sysUserDao = sysUserDao;
}
@RequestMapping("login")
public ModelAndView getSysUser(String userId, String psd) {
<!-- 方法体 本方法必须返回一个 return new ModelAndView( 需要返回的东西 );-->
return new ModelAndView(this.getSuccessView(), sysUserMap);
} else {
return new ModelAndView(this.getError());
}
}
}
需要的包见下图