springmvc之前后台传值

时间:2023-03-10 00:04:43
springmvc之前后台传值

一、向后台传值

1、项目结构

springmvc之前后台传值

2、jar包

springmvc之前后台传值

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间跳转 重定向 传参 (转)