Springmvc入门案例(1)

举报
穆雄雄 发表于 2022/12/17 11:23:15 2022/12/17
【摘要】 ​ 据说,现在springmvc火了,好多企业都在使用,既然这样,咱们也得会点,于是乎就开始自学了,通过找资料,终于做出来了一个简单案例,这里分享供大家浏览,主要分为以下几个步骤:一、springmvc是一个mvc框架,通过mvc很好的将数据、业务、表现进行分离二、springmvc是围绕DispatcherServlet展开的,由DispatcherServlet负责将请求派发到特定的ha...

 据说,现在springmvc火了,好多企业都在使用,既然这样,咱们也得会点,于是乎就开始自学了,通过找资料,终于做出来了一个简单案例,这里分享供大家浏览,主要分为以下几个步骤:

一、springmvc是一个mvc框架,通过mvc很好的将数据、业务、表现进行分离
二、springmvc是围绕DispatcherServlet展开的,由DispatcherServlet负责将请求派发到特定的handler
springmvc框架搭建步骤:
    1.创建项目,拷贝springmvc相关的jar包。
    2.配置web.xml主要配置前端控制器DispatcherServlet
      

 <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>



    3.在web-inf下创建springmvc配置文件,[servletname]-servlet.xml,文件名必须是springmvc-servlet.xml
    4.配置HandlerMapping映射,根据beanname找到对应的Controller(可以省略)
      


 <bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping">


    5.创建jsp页面,需要请求发出请求的页面
    6.创建Controller,继承AbstractController,重写handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response)
      

 //返回数据和页面
        @Override
        protected ModelAndView handleRequestInternal(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
            String hello = request.getParameter("file");
            System.out.println(hello);
            ModelAndView modelAndView = new ModelAndView("welcome");    //返回到的页面
            modelAndView.addObject("helloword",hello);                    //返回值
            return modelAndView;
        }    



    7.配置视图解析器
  


 <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置前缀和后缀 -->
        <property name="prefix" value="/"></property>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>


下面我把源码放上来吧;


    web.xml里面的配置:
        

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <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>
   <param-value>/WEB-INF/pringMVC-servlet.xml</param-value>
  </init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>


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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	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-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	<!-- 配置HandlerMapping映射,根据beanname找到对应的Controller -->
	<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean>
	<!-- 配置Controller -->
	<bean id="hello.do" class="org.controller.HelloController"></bean>
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置前缀和后缀 -->
		<property name="prefix" value="/"></property>
		<!-- 后缀 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>


    HelloController控制器里面的代码:



	 /**  
	* @Title: HelloController.java
	* @Package org.controller
	* @Description: TODO该方法的主要作用:
	* @author A18ccms A18ccms_gmail_com  
	* @date 2017-8-30 下午8:38:53
	* @version V1.0  
	*/
	package org.controller;
	
	import javax.servlet.http.HttpServletRequest;
	import javax.servlet.http.HttpServletResponse;
	
	import org.springframework.web.servlet.ModelAndView;
	import org.springframework.web.servlet.mvc.AbstractController;
	
	
	 /**   
	 *    
	 * 项目名称:springmvc_day1   
	 * 类名称:HelloController   
	 * 类描述:   控制器
	 * 创建人:Mu Xiongxiong  
	 * 创建时间:2017-8-30 下午8:38:54   
	 * 修改人:Mu Xiongxiong   
	 * 修改时间:2017-8-30 下午8:38:54   
	 * 修改备注:   
	 * @version    
	 *    
	 */
	public class HelloController extends AbstractController {
	
		@Override
		protected ModelAndView handleRequestInternal(HttpServletRequest request,
				HttpServletResponse response) throws Exception {
			String hello = request.getParameter("file");
			System.out.println(hello);
			ModelAndView modelAndView = new ModelAndView("/welcome");	//返回到的页面
			modelAndView.addObject("helloword",hello);					//返回值
			return modelAndView;
		}
		
		
	
	}


index.jsp里面的代码:



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <form action="hello.do" method="post">
    	hello:<input type="text" name="file"/><br/>
    	<input type="submit" value="提交"/>
    </form>
  </body>
</html>


welcome.jsp的代码:



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    hello:${helloword }
  </body>
</html>


最后看一下目录对应关系:


运行看welcome.jsp页面:


编辑

运行完毕,遇到的问题:

Springmvc中提交from之后不跳转不进控制器 
 Could not open ServletContext resource [/WEB-INF/springmvc-servlet.xml]【解决方案】

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。