SpringMVC基础框架搭建
SpringMVC框架搭建步骤:
1、将需要jar包导入lib文件夹下 2、配置web.xml 3、配置springMVC核心配置文件 4、编码Controller类
说明:本项目源码导入eclipse,在tomcat运行后 输入http://localhost:8080/BrainTrain/welcome.jsp进行测试
所需的jar包:http://pan.baidu.com/s/1i3QKYNF(百度云盘)
项目源码:http://pan.baidu.com/s/1kTWM9Rh;(百度云盘)
一、配置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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>BrainTrain</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--springMVC配置文件地址,config是src下的包 -->
<param-value>classpath*:config/springAnnotation-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
二、配置springMVC核心配置文件:springAnnotation-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 注解扫描包 -->
<context:component-scan base-package="com.chuck.codeResource"></context:component-scan>
<!-- 开启注解 -->
<mvc:annotation-driven/>
<!-- 静态资源访问 -->
<mvc:resources location="/img/" mapping="/img/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/staticHtml/" mapping="/staticHtml/**"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
三、编写Controller类
package com.chuck.codeResource.user;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
@Controller
@RequestMapping("/user")
public class LoginController extends MultiActionController {
@RequestMapping("/addUser")
public String addUser(HttpServletRequest request,HttpServletResponse response){
System.out.println("-----add----");
String result="this is addUser";
request.setAttribute("result",result);
return "/welcome";
}
@RequestMapping("/delUser")
public String delUser(HttpServletRequest request,HttpServletResponse response){
System.out.println("-----delUser----");
String result="this is delUser";
request.setAttribute("result",result);
return "/welcome" ;
}
@RequestMapping("/updateUser")
public String updateUser(HttpServletRequest request,HttpServletResponse response){
System.out.println("-----update----");
String result="this is updateUser";
request.setAttribute("result",result);
return "/welcome";
}
}
四、配置访问的jsp页面:welcome.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
本次执行的方法是:${result}
<form action="user/addUser" method="post">
<input type="submit" value="post"/>
</form>
</body>
</html>