springMVC入门小程序
①新建maven项目
②添加web框架
③项目结构-Artifacts-新建目录lib-导入jar包
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- SpringMVC核心控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Servlet的初始化参数,读取springmvc的配置文件,创建spring容器-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 配置servlet启动时加载对象-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 解决中文乱码-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 指定字符集-->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 批量扫描包-->
<context:component-scan base-package="com.my"/>
<!-- 配置视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置自定义类型转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.my.utils.StringToDateConverter"/>
</set>
</property>
</bean>
<!-- 配置spring开启注解mvc的支持-->
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- 让Spring MVC不处理静态资源 -->
<mvc:default-servlet-handler />
</beans>
/WEB-INF/pages/param.jsp
<%--
Created by IntelliJ IDEA.
User: ASUS
Date: 2020/9/6
Time: 15:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/save" method="post">
姓名:<input type="text" name="user.userName"/><br>
年龄:<input type="text" name="user.age"/><br>
性别:<input type="text" name="user.sex"/><br>
出生年月:<input type="text" name="user.birth"/><br>
爱好:<input type="text" name="hobby"/><br>
期望薪资:<input type="text" name="money"/><br>
<input type="submit" value="提交">
</form>
</body>
</html>
java代码:
package com.my.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author Heilige Scheisse
* @version 1.0
* @date 2020/9/6 16:59
*/
@Controller
@RequestMapping("/anno")
public class AnnoController {
@RequestMapping("/testRequestParam")
public String test01(String userName) {
System.out.println("执行了");
System.out.println(userName);
return "success";
}
@RequestMapping("/jump")
public String test() {
System.out.println("执行了");
return "anno";
}
}
8080 is aleraly use解决方法:
netstat -ano | findstr 8080
taskkill /F /pid 56664