使用springmvc配置servlet
文章目录
将springmvc引入系统
设置web.xml
在web.xml中
添加servlet的相关配置,进而引入springmvc
springmvc提供了一个servlet,能够拦截所有的请求
servlet-mapping设置了servlet的检查路径,路径设置为"/"
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>HdrSpringWork</display-name>
<!-- springmvc提供了一个servlet,能够拦截所有的请求,也就是说所有的请求都经过它 其映射方式类似于 /* -> dispatcherServlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
</web-app>
设置配置springmvc的相关文件
springmvc的配置中提到了配置springmvc的配置文件(配置springmvc的工作要求)
<param-value>classpath:spring/dispatcher-servlet.xml</param-value>
按照路径在main->resource中建立文件夹spring、db(用于存储数据库相关配置文件)、mapper(用于存储映射相关文件)
注:resource文件中存放的是Java代码运行所依赖的资源,早期的resource文件中一般放properties配置文件,现在大部分转化为xml文件(xml文件表达能力更强)
**注:resource文件打包时也在类路径下,右键点击项目properties–Deployment Assembly即可查看 **
在文件夹spring中新建dispatcher-servlet.xml
现在的springmvc的配置不再是以前逐条书写配置文件,而是改用注解来达到配置的效果(注解驱动)。
-
在文件中启用注解配置
-
在文件中指出控制器所在位置(对应的控制器Java包路径)(base-package属性)
且在控制器包中的控制器类前添加注解
@Controller
springmvc即可识别
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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.3.xsd">
<!-- 本文件指定了springmvc需要处理的内容的处理机制,如果不是springmvc处理的,则一律放行 -->
<!-- springmvc的配置是用注解就可以了,十分方便 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 控制器部件扫描 (springmvc中servlet已经被controller这个概念封装掉了)
我们必须让springmvc知道其控制器所在的位置,以便加载控制器
包含基本包下头的子包
-->
<context:component-scan base-package="edu.mju.hdrwork.controller"></context:component-scan>
</beans>
新建controller包
- 新建包,包名:edu.mju.hdrwork.controller
- 在包中新建控制器类,类名:HairdryerController
- 在新建的类中(类名上一行)添加
@Controller
注解 - 在类中新建一个test方法,在方法前添加
@RequestMapping("/a")
注解。其中"/a"代表方法对应的映射路径 - 通过注解
@ResponseBody
结合Java返回字符串,将指定字符串返回至网页进行测试
/**
*
*/
package edu.mju.hdrwork.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author XKF
*
*/
@Controller
public class HairdryerController {
@RequestMapping("/a")
@ResponseBody
public String test() throws Exception{
System.out.println("test now!!!");
return "test hhhhhhhhhh!";
}
}
配置log4j
-
为了看到启动springmvc时打印的信息,使用log4j(将log4j.properties配置文件放置到resource文件夹下)
log4j:the logging tools for(four) java
for——four(谐音)
log4j是专门为Java开发的日志工具,几乎得到所有程序员认可,成为了工业标准。
-
到Maven Repository网站当中查询log4j(Java包),拷贝相应包路径,粘贴到pom.xml。
-
在Maven Repository网站中搜索slf4j(Java包),拷贝包路径,粘贴到pom.xml。
-
在网站中继续搜索需要的包,并拷贝包路径,粘贴到pom.xml中。
配置网页(略)
将服务器连接到网页
继续编辑控制器类
-
设置函数toInput,并在函数前添加
@GetMapping
GetMapping和ResponseMapping的区别,GetMapping仅处理get提交,response处理所有提交。/** * */ package edu.mju.hdrwork.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * @author XKF * */ @Controller public class HairdryerController { @RequestMapping("/a") @ResponseBody public String test() throws Exception{ System.out.println("test now!!!"); return "test hhhhhhhhhh!"; } @GetMapping("/hdr/input") public String toInput() throws Exception{ System.out.println("input!"); //返回一个视图,他的名字叫input_hairdryer return "input_hairdryer.html"; } }
-
由于方法返回了一个视图,我们需要告诉系统去何处找这个视图,因此我们需要配置视图解析器。
配置视图解析器
编辑springmvc配置文件
-
在配置文件中添加视图解析器配置
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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.3.xsd"> <!-- 本文件指定了springmvc需要处理的内容的处理机制,如果不是springmvc处理的,则一律放行 --> <!-- springmvc的配置是用注解就可以了,十分方便 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 控制器部件扫描 (springmvc中servlet已经被controller这个概念封装掉了) 我们必须让springmvc知道其控制器所在的位置,以便加载控制器 包含基本包下头的子包 --> <context:component-scan base-package="edu.mju.hdrwork.controller"></context:component-scan> <!-- 该视图解析器能够获取controller方法返回的视图信息,形成一个完整的页面路径 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
-
其中"/WEB-INF/views/“表示文件路径“前缀”,”.jsp"表示文件路径“后缀”,也可以将后缀部分删除,在方法的return中自己添加后缀
若要视图解析的目标是html,需要配置静态资源,否则无法解析。
接下来配置 -
修改后
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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.3.xsd"> <!-- 本文件指定了springmvc需要处理的内容的处理机制,如果不是springmvc处理的,则一律放行 --> <!-- springmvc的配置是用注解就可以了,十分方便 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 控制器部件扫描 (springmvc中servlet已经被controller这个概念封装掉了) 我们必须让springmvc知道其控制器所在的位置,以便加载控制器 包含基本包下头的子包 --> <context:component-scan base-package="edu.mju.hdrwork.controller"></context:component-scan> <!-- 该视图解析器能够获取controller方法返回的视图信息,形成一个完整的页面路径 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> </bean> <mvc:default-servlet-handler/> </beans>
-
因为html文件中导入了bootstrap等文件,因此我们需要将导入的资源做静态处理,再次修改配置文件
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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.3.xsd"> <!-- 本文件指定了springmvc需要处理的内容的处理机制,如果不是springmvc处理的,则一律放行 --> <!-- springmvc的配置是用注解就可以了,十分方便 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 控制器部件扫描 (springmvc中servlet已经被controller这个概念封装掉了) 我们必须让springmvc知道其控制器所在的位置,以便加载控制器 包含基本包下头的子包 --> <context:component-scan base-package="edu.mju.hdrwork.controller"></context:component-scan> <!-- 该视图解析器能够获取controller方法返回的视图信息,形成一个完整的页面路径 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> </bean> <mvc:default-servlet-handler/> <!-- 静态资源 --> <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources> </beans>
-
新建一个与WEB-INF文件夹同级,且名为resources的文件夹,用于存储bootstrap等文件。同时在网页中配置好链接路径。
最后运行网页,将网页地址后面添加hdr/input测试结果。