SpringMVC入门

一、web.xml文件配置走起
(先将以下包导入项目)
 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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">
 3   <display-name>lx0602</display-name>
 4   
 5   <!-- 运行时首先进入的页面为index.jsp -->
 6   <welcome-file-list>
 7     <welcome-file>index.jsp</welcome-file>
 8   </welcome-file-list>
 9   
10   <!-- 对spingmvc配置(spingmvc的核心控制器) -->
11   <servlet>
12       <!--servlet-name随便起但要和servlet-mapping的一样-->
13       <servlet-name>springmvc</servlet-name>
14       <!--加载sringmvc主配置文件-->
15       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16       <!--解析src包下的**xml>
17       <init-param>
18          <!--contextConfigLocation师承DispatcherServlet的接口,不能随便起名字-->
19             <param-name>contextConfigLocation</param-name>
20             <param-value>classpath:springmvc-servlet.xml</param-value>
21            
22         </init-param>
23          <!--加载优先级-->
24          <!--预先创建-->
25         <load-on-startup>1</load-on-startup>
26   </servlet>
27 
28   <servlet-mapping>
29       <servlet-name>springmvc</servlet-name>
30       <!--拦截后缀为.do的请求 -->
31       <url-pattern>*.do</url-pattern>
32   </servlet-mapping>
33 </web-app>
二、在项目src下编写springmvc-servlet.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 http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> 
        
        
     <!-- 开启springmvc -->                      
    <context:annotation-config />
    <!-- 解析以下包及其子包 -->
    <context:component-scan base-package="test.SpringMVC"/>
    <!-- 对页面路径进行解析 -->
    <!-- 及在页面使用return加字符串跳转到相应页面-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
 
   
</beans>
三、编写Controller文件(及上述context解析包test.SpringMVC里的内容)
 1 @Controller
 2 @RequestMapping("/user")
 3 //在这里可以把以下每个类理解为一个文件而@RequestMapping("/user")为装这些文件的文件夹
 4 public class test {
 5     @RequestMapping("hello.do")
 6     public String hello(){
 7         return "redirect:hello";
 8         
 9     }
10     @RequestMapping("add.do")
11     public String add(String name){
12         
13         System.out.println(name);
14         return "hello";
15     }
16 
17 }
//以上类还可以这么写(作用和以上相同但比较麻烦)
 1 public class test {
 2     @RequestMapping("hello.do")
 3     public ModelAndView hello(){
 4      //创建一个ModelAndView及视图模型盒子(里边存储的是键值对)
 5         ModelAndView mv = new ModelAndView();
 6               //封装数据
 7         mv.addObject("Welcome","HEllow word!!");
 8               //跳转页面
 9               mv.setViewName("hello");
10               //请求发送路径
11               return mv;
12     }
四、其他文件(及jsp文件内的文件和index文件)
jsp/hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hello word!!
</body>
</html>

index.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <form action="user/add.do">
11     姓名:<input name="name" type="text">
12     <input type="submit" value="提交">
13 
14 
15 </form>
16 </body>
17 </html>
//如果以上两个jsp文件都在jsp文件夹下提交请求时可能导致/jsp/jsp的情况
//以下为获取所在项目名称的方法
action="${pageContext.request.contextPath}"
action="<%=request.getcontextPath %>"

项目列表

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/laogao2333/p/14842744.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程