1. 程式人生 > >2018 SpringMVC 配置及使用

2018 SpringMVC 配置及使用

一、pom.xml

<!--引用springMVC依賴-->
<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version>5.1.0.RELEASE</version>
</dependency>

二、springmvc-config.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:content="http://www.springframework.org/schema/context"
       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
">
    
    <!--掃描註解-->
    <content:component-scan base-package="com.baidu.mybatis.controller"/>

</beans>

三、web.xml

    <!--定義Spring MVC的前端控制器-->
    <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-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!--攔截的規則-->
        <url-pattern>*.xlm</url-pattern>
    </servlet-mapping>

四、LoginController.java

package com.baidu.mybatis.controller;

import com.baidu.mybatis.pojo.TbUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * Created by IntelliJ IDEA.
 *
 * @author xlm
 * description:
 * date: 2018/10/15 15:51
 * version: 02.06
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class LoginController {
    
    @RequestMapping("/login")
    public String login(String userName, String userUpwd) {

        session.setAttribute("userName",userName);

        session.setAttribute("userUpwd",userUpwd);

        return "redirect:/success.jsp";
    }

}

五、JSP

1.index.jsp

<%--
  Created by IntelliJ IDEA.
  User: xlm
  Date: 2018/9/29
  Time: 9:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>Title</title>
</head>

<script src="js/jquery-3.3.1.js" type="text/javascript"></script>

<script type="text/javascript">
    function test() {

        window.location.href = "/login.xlm?"+$('#LoginForm').serialize();

    }
</script>

<body style="background: url('images/07.jpg');">

    <form id="LoginForm" method="post">
        <span id="xlm" onclick="test()" style="margin-right: 8px;margin-left: 8px;"></span>
        <input id="userName" name="userName" type="text" placeholder="請輸入帳號" style="margin-right: 10px;" />
        <input id="userUpwd" name="userUpwd" type="password" placeholder="請輸入密碼" />
    </form>

</body>

<script type="text/javascript">

    $('#xlm').html("檸澤");

</script>

</html>

2.success.jsp

<%--
  Created by IntelliJ IDEA.
  User: xlm
  Date: 2018/10/15
  Time: 15:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>成功頁面</title>
</head>
<body style="background: url('images/07.jpg');">

    <div>您的帳號為:${userName}</div> <div>您的密碼為:${userUpwd}</div>

</body>
</html>

六、ResultView

1.index.jsp

2.success.jsp