springmvc之前后台傳值


一、向后台傳值

1、項目結構

2、jar包

3、spring-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:oxm="http://www.springframework.org/schema/oxm"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/oxm
       http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
         http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task-3.2.xsd">

    <!--  通知spring容器通過注解的方式裝配bean --> 
      <context:annotation-config /> 
    <!--  通知spring容器采用自動掃描機制查找注解的bean --> 
      <context:component-scan base-package="com.*" /> 
      
      <task:annotation-driven /> <!-- 定時器開關-->
      
      <bean id="agentExcelTask" class="com.timer.TimerController1"/>
    <task:scheduled-tasks>
        <task:scheduled ref="agentExcelTask" method="printstr" cron="* * 0/1000 * * ?"/>
    </task:scheduled-tasks>  
    
    <!--  配置返回頁面過濾 --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

4、LoginController.java,下面是spring向后台傳值的三種方式。

package com.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.demo.User;

@Controller
public class LoginController {
    
    /**
     * 使用HttpServletRequest獲取
     */
    @RequestMapping("/login1")
    public String login1(HttpServletRequest request,Model model){
        model.addAttribute("name", request.getParameter("name"));
        model.addAttribute("password", request.getParameter("password"));
        return "success";
    }
    
    /**
     * spring自動將表單參數注入到方法參數,參數值和頁面name屬性一致時可以省去@RequestParam注解
     */
    @RequestMapping("/login2")
    public String login2(@RequestParam("name") String name, String password,Model model){
        model.addAttribute("name", name);
        model.addAttribute("password", password);
        return "success";
    }
    
    /**
     * 自動注入bean屬性
     */
    @RequestMapping("/login3")
    public String login3(User user,Model model){
        model.addAttribute("name", user.getName());
        model.addAttribute("password", user.getPassword());
        return "success";
    }

}

注:第三種方式自動注入bean屬性需要定義實體類,本例中定義User.java

5、User.java

package com.demo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {
    
    @Id
    @GeneratedValue
    private long id;
    private String name;
    private String password;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    
}

6、index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%
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>index</title>
  </head>
  
  <body>
    <form action="login2" method="post">
          用戶:<input type="text" name="name"><br><br>
          密碼:<input type="text" name="password"><br><br>
        <input type="submit" value="確定">
    </form>
    
    <!-- 使用message 標簽配置需要顯示的國際化文本, 
           code  對應國際化文件中對應的鍵的名稱  -->
    <span style="color: #2D2D2D;">
        <spring:message code="main.title"/>
    </span>
    <br>
    <input type="text" value="<spring:message code="main.target"/>">  
  </body>
</html>

7、success.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>success</title>
  </head>
  
  <body>
    ${name},success. <br>
    用戶名:${name},密碼:${password}
  </body>
</html>

 二、向前台傳值的兩種方式

package com.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.demo.User;

@Controller
public class LoginController {
    
    @RequestMapping("/login2")
    public String login2(@RequestParam("name") String name, String password,Model model){
        model.addAttribute("name", name);
        model.addAttribute("password", password);
        return "success";
    }
    
    return "success";
    }
    
    @RequestMapping("/login4")
    public String login4(User user, Map<String, Object> map){
        map.put("name", user.getName());
        map.put("password", user.getPassword());
        return "success";
    }

}

 三、springmvc重定向

參考spring mvc controller間跳轉 重定向 傳參 (轉)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM