歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

SpringMVC的簡單示例

首先導入所需的jar包,項目目錄結構如下:

之後需要配置一下web.xml文件,內容如下:

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

然後配置applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 配置自動掃描的包 -->
    <context:component-scan base-package="com.sprmvc.po"></context:component-scan>
    <!-- 配置視圖解析器 將HelloWorldController中的返回值解析為實際的物理視圖 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- prefix為前綴,suffix為後綴 -->
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

下面開始建立實體類User.java:

package com.sprmvc.po;

public class User {
    private String userName;
    private String password;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

然後是控制層代碼:

package com.sprmvc.po;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloWorldController {
    @RequestMapping(value="/hello",method = RequestMethod.GET)
    public String printHelloWorld(HttpServletRequest request,User user) {
        request.setAttribute("userName", user.getUserName());
        request.setAttribute("password", user.getPassword());
        return "hello";
    }
}

這裡的value即訪問路徑,而return的"hello"通過applicationContext.xml中配置的視圖解析器會返回到hello.jsp中

接下來我們建立兩個.jsp頁面,首先是index.jsp:

<body>
    <form method="get" action="hello">
    用戶名:<input type="text" name="userName">
    密碼:<input type="password" name="password"><br>
        <input type="submit" value="提交">
    </form>
  </body>

這裡的method和action分別與控制層中的method和value的值相對應,即HelloWorldController.java中的第11行。

然後是hello.jsp:

1 <body>  
2 <h1>操作成功了</h1>  
3 您的用戶名為:${userName}<br>
4 您的密碼為:${password }  
5 </body>  

這樣只需要將項目加載到tomcat下就可以進行訪問了,趕快試試吧。

SpringMVC+MyBatis集成配置  http://www.linuxidc.com/Linux/2016-09/135212.htm

SpringMVC總結篇  http://www.linuxidc.com/Linux/2016-06/132659.htm

Spring+SpringMVC企業快速開發架構搭建  http://www.linuxidc.com/Linux/2015-09/122942.htm

SpringMVC的亂碼處理  http://www.linuxidc.com/Linux/2015-07/120542.htm

Spring MVC+Spring3+Hibernate4開發環境搭建 http://www.linuxidc.com/Linux/2013-07/87119.htm

Spring MVC整合Freemarker基於注解方式 http://www.linuxidc.com/Linux/2013-02/79660.htm

基於注解的Spring MVC簡單介紹 http://www.linuxidc.com/Linux/2012-02/54896.htm

SpringMVC詳細示例實戰教程 http://www.linuxidc.com/Linux/2015-06/118461.htm

Spring MVC 框架搭建及詳解 http://www.linuxidc.com/Linux/2012-01/52740.htm

SpringMVC 異常處理  http://www.linuxidc.com/Linux/2015-06/119049.htm

SpringMVC框架入門配置 IDEA下搭建Maven項目 http://www.linuxidc.com/Linux/2016-09/134918.htm

Copyright © Linux教程網 All Rights Reserved