SpringMVC_环境搭建

SpringMVC框架搭建:

1.导入jar包

2.书写主配置文件:springmvc.xml  web.xml

3.启动tomcat测试

准备工作:

新建Dynamic Web Project,下一步下一步勾选生成xml

项目下新建source folder名为resource。src下创建包cn.java.controller

在Eclipse里配置jsp文件的模板,window-preference-jsp file - templates-把jsp html那个编辑成如下,以后生成的jsp就都是这个了

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
<%
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() +request.getContextPath()+"/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath %>">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>new jsp</title>
</head>
<body>
    
</body>
</html>

WebContext下创建jsp文件名为index.jsp然后运行测试是否可用

第一步:导入jar包

把这些jar包复制到WebContent下WEB-INF下的lib中

第二步:书写主配置文件:springmvc.xml  web.xml

在resource文件夹中创建springmvc.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:mvc="http://www.springframework.org/schema/mvc"
    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.2.xsd 
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
            
            
            
</beans>

先在web.xml中配置springMVC的核心控制器类:DispatcherServlet 

面试题:Struts和SpringMVC核心控制器的区别?

答:SpringMVC核心控制器是DispatcherServlet,属于Servlet,Struts核心控制器是过滤器。

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_3_0.xsd" 
id="WebApp_ID" 
version="3.0">
  <display-name>springMVC</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-list>
  
  <!-- 配置springMVC的核心控制器类:DispatcherServlet -->
  <servlet>
      <servlet-name>dispatcherServlet</servlet-name>
      <!-- 按住ctrl + shift + H 输入DispatcherServlet,ok后在文件右键复制路径,粘贴到下面 -->
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- 将DispatcherServlet和springmvc.xml文件关联起来 -->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <!-- param-value:给springmvc起一个文件名,固定写法,不能改 -->
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>dispatcherServlet</servlet-name>
      <!-- /*代表全部都拦截,/代表当前项目,*代表所有的,  如果是*.html,那就拦截所有html文件 -->
      <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

springmvc.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:mvc="http://www.springframework.org/schema/mvc"
    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.2.xsd 
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
    
    <!-- 配置包扫描 -->        
    <context:component-scan base-package="cn.java.controller"></context:component-scan>
        
    <!-- 加入springmvc特有的注解驱动 -->    
    <mvc:annotation-driven></mvc:annotation-driven>    
    
</beans>

在一个普通类上一行加上@Controller就变成了Servlet了,就无需extends继承Servlet了。

package cn.java.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller // 在一个普通类上一行加上@Controller就变成了Servlet了,就无需extends继承Servlet了。
public class FrontController {
    
    @RequestMapping(value="/login.html") //为当前方法配置一个对外访问的虚拟路径,@RequestMapping是springmvc框架特有的,并可以处理get和post请求
    public void Login() {
        System.out.println("登录成功");
    }
    
    
    //Servlet只能写一个方法,而用了springmvc可以写多个方法
    @RequestMapping(value= {"/register.html","abc.html","hello.html"})  //可以配置很多个虚拟路径
    public void Register() {
        System.out.println("注册成功");
    }
    
    @RequestMapping(value= "/logon.html") 
    public void Logon(String username,String password) { //在进入地址http://localhost:8080/springMVC/logon.html?username=wang&password=123后得到的结果直接把username的值和password的值传到方法里去了,而Servlet是要用request来取值的,springmvc简单了很多
        System.out.println("用户名为" + username);
        System.out.println("密码为" + password);
    }
    
    @RequestMapping(value= "/logon2.html") 
    public void Logon2(User u) { //在进入地址http://localhost:8080/springMVC/logon2.html?username=wang&password=123后得到的结果直接把username的值和password的值传到类名为User中去了,User类中写好了username和password两个属性,还有setter getter方法,可以直接传进User对象中去,并且可以转好类型
        System.out.println(u);
    }
}    
原文地址:https://www.cnblogs.com/lonske/p/9094365.html