使用原生web.xml配置springMVC项目,controller层返回字符串,映射到页面的详细过程

1.使用原生web.xml配置springMVC项目
参考:https://blog.csdn.net/qq_22764659/article/details/111354881

整体预览项目结构
![在这里插入图片描述](https://img-blog.csdnimg.cn/e768ebee0d9c41e6bfa70d0ba943365f.png#pic_center)

2.配置web.xml中的配置
web.xml中的配置,配置如下, 主要是引入DispatcherServlet,这个是springMVC项目访问的入口。

<?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:jsp="http://java.sun.com/xml/ns/javaee/jsp" 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">
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
 <!-- log4j2的配置文件 -->
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
	</context-param>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.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>
  <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>
  <jsp-config>
    <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
      <el-ignored>false</el-ignored>
      <scripting-invalid>true</scripting-invalid>
    </jsp-property-group>
  </jsp-config>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

3.在上面的web.XML里面,引入springMVC.xml的配置
设置controller转发时的前缀后缀。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!--开启注解扫描 -->
	<context:component-scan base-package="com.esquel">
		<!--只扫描控制器 -->
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<!--加载项目中的静态资源的两种方式: -->
	<!-- 使用MVC的默认servlet(该处理器其实就是调用tomcat的defaultservlet) -->
	<!-- -Spring MVC 在全局配置文件中提供了一个mvc:default-servlet-handler标签。 在 WEB 容器启动的时候会在上下文中定义一个 
		DefaultServletHttpRequestHandler 它会对DispatcherServlet的请求进行处理,如果该请求已经作了映射,那么会接着交给后台对应的处理程序, 
		如果没有作映射,就交给 WEB 应用服务器默认的 Servlet 处理,从而找到对应的静态资源,只有再找不到资源时才会报错。 -->
	<mvc:default-servlet-handler />
	<!-- 方式2:mapping存放的请求的url location存放的地址 推荐使用 -->
	<!--<mvc:resources location="/images" mapping="**/images/**" /> -->
	<!--<mvc:resources location="/css" mapping="**/css/**" /> -->
	<!--<mvc:resources location="/fonts" mapping="**/fonts/**" /> -->
	<!--<mvc:resources location="/js" mapping="**/js/**" /> -->
	<!-- 配置mvc拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/product*.html" />
			<bean class="com.XXX.interceptor.Interceptor" />
		</mvc:interceptor>
	</mvc:interceptors>

	<!--设置controller转发时的前缀后缀 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/page/" />
		<property name="suffix" value=".html" />
		<property name="contentType" value="text/html;charset=UTF-8"></property>
	</bean>

	<!--上传文件配置 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="UTF-8" />
		<!--设置上传文件大小最大为10M -->
		<property name="maxUploadSize" value="51200000" />
	</bean>


	<mvc:annotation-driven />
</beans>		

4.Controller的servlet ,自动映射到对应的网页过程
在Controller里面,定义了一个servlet ,返回一个字符串。系统在执行完该servlet 后,自动映射到对应的网页(自动打开对应网页)

举例:下面的servlet 方法resetPassword,其返回了字符串"resetPassword",那么系统在执行完该servlet 后,会根据上面步骤3的配置,打开/WEB-INF/page/resetPassword.html文件

// 密码重置
	@RequestMapping("/resetPassword")
	public String resetPassword(HttpServletRequest request,
			HttpSession session, HttpServletResponse response, String email,
			String sign) throws IOException {

		String msg = checkResetPasswordSign(sign, email);
		if (!msg.equals("")) {
			String result = "<script language='javascript'>alert('" + msg
					+ "');window.location='index.html';</script>";
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.print(result);
			return null;
		}

		session.setAttribute("email", email);
		session.setAttribute("sign", sign);

		return "resetPassword";
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值