JAVA前端与后端参数传递方法小结

1,从Action中传值到JSP页面的方法

①在Action中定义一个成员变量,然后对这个成员变量提供get/set方法,在JSP页面就可以取到这个变量的值了。

  1)在Action中定义成员变量
//定义一个成员变量
private String message;
//提供get/set方法
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
  
2)在JSP页面中取值
${message}或者
<s:property value="message"/>


②使用一些Servlet API进行值的存取操作:HttpServletRequest、HttpSession和ServletContext。Struts2对这个三个对象用Map进行了封装,我们就可以使用Map对象来存取数据了。
  
1)在Action中存值
ActionContext actionContext = ActionContext.getContext(); //get HttpServletRequest
Map<String,Object> request = (Map) actionContext.get("request");
request.put("a", "a is in request");
//get HttpSession
//Map<String,Object> session = (Map) actionContext.get("session");
Map<String,Object> session = actionContext.getSession();
session.put("b", "b is in session");

//get ServletContext
//Map<String,Object> application = (Map) actionContext.get("application");
Map<String,Object> application = actionContext.getApplication();
application.put("c", "c is in application");

//get ServletActionContext.request
HttpServletRequest request=ServletActionContext.getRequest()
request.setAttribute("c2", "test5");
request.put("c3","test6")
//或者直接放入上下文中
ActionContext.getContext().put("d","d is in application");

 2)在JSP页面上取值(使用EL表达式)

${a}
${b}
${c}
${d}
${c2}
${c3}
or
${requestScope.a}
${sessionScope.b}
${applicationScope.c}
${actionContext.d}
${requestScope.c2}${requestScope.c3}
or<%=request.getAttribute("d")%> //使用ActionContext.getContext().put("d","d is an application")的情况
附加:ServletContext,ActionContext,ServletActionContext的区别


在ActionContext.getContext().put("a", " test3")后页面上用${a}或<s:property value="#a"/>获得test3的值

#相当于ActionContext. getContext() ,#session.b表达式相当于ActionContext.getContext().getSession(). getAttribute(”b”)


③对于传递list的值,可以使用 actionContext的valueStack来传递值栈

1) 在后台使用Hibernate查询 ,EntityManager 通过createQuery()来getResultList()获得List,将list放入valueStack中


ActionContext.getContext().getValueStack().push(model);
2)在页面上通过<s:iterator>标签遍历list的每条值显示在table上。


<table class="tablelist" >
<thead>
<tr>
<th width="100px;">编号</th>
<th>影片类型</th>
<th>影片名称</th>
<th>发布人</th>
<th>发布时间</th>
<th>审核状态</th>
<th>操作</th>
</tr>
</thead>

<tbody>
<s:iterator value="recordList" var="o" status="i">
<tr>
<td>${o.id}</td>
<td>${o.name }</td>
<td>${o.type}</td>
<td>${o.updateMember.memberName }</td>
</tr>
</s:iterator>
</tbody>

</table>


2,从前台向后台传递参数

①通过表单传递参数

1)在前端jsp页面设置form表单,确定需要传递的参数name让用户在input中输入,通过点击按钮后submit()提交到后台

<s:form method="post" action="ActivityAction_toUI.action">
<table class="serTable">
<tbody>
<tr>
<td>
<label>活动名称</label>
</td>
<td>
<input type="text" class="serput" name="activityName" placeholder="输入文本"/>
</td>
<td>
<s:submit cssClass="btn1" value="搜索" οnclick="submit();" />
</td>
</tr>
</tbody>
</table>
</s:form>
2)点击搜索后activityName会放到HttpServletRequest 中

HttpServletRequest httpReq = ServletActionContext.getRequest();
String s = httpReq.getParameter("activityName");
另外,在后台也可以通过extends ActionSupport 并构建get/set方法在后台获得其值
private String activityName;

public String getActivityName() {
return activityName;
}

public void setActivityName(String activityName) {
this.activityName = activityName;
}

public String list(){
System.out.println(activityName);
}


②通过超链接传递参数

1)前台通过超链接跳转时将参数加到方法的后面


<s:a cssClass="acolor" target="mainBody" theme="simple" action="ActivityAction_info.action?Id=%{Id}&pageNum=%{pageNum}&infot=\"mylittlepony\"" ><img
src="${pageContext.request.contextPath}/images/info.png"
title="查看" /></s:a>
2)后台通过HttpServletRequest 获得超链接后面参数所对应的值

HttpServletRequest httpReq = ServletActionContext.getRequest();
<pre name="code" class="java">Long id= Long.parseLong(httpReq.getParameter("Id"));String s = httpReq.getParameter("infot");
Long pageNum = Long.parseLong(httpReq.getParameter("pageNum"));
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用Vue.js和Java编写的前端后端传递参数的例子: 前端代码(使用Vue.js): ```html <!DOCTYPE html> <html> <head> <title>Vue Example</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> new Vue({ el: '#app', data: { name: '', age: '', response: '' }, methods: { sendData() { axios.post('backend.php', { name: this.name, age: this.age }) .then(response => { this.response = response.data; }) .catch(error => { console.error(error); }); } } }); </script> </head> <body> <div id="app"> <input type="text" v-model="name" placeholder="Name"> <input type="text" v-model="age" placeholder="Age"> <button @click="sendData">Send Data</button> <div>{{ response }}</div> </div> </body> </html> ``` 后端代码(Java,使用Spring Boot): ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class BackendApplication { public static void main(String[] args) { SpringApplication.run(BackendApplication.class, args); } @PostMapping("/backend.php") public String processData(@RequestBody RequestData requestData) { String name = requestData.getName(); int age = requestData.getAge(); // 处理数据,这里只是简单地将数据拼接成字符串作为响应返回 String response = "Name: " + name + ", Age: " + age; return response; } public static class RequestData { private String name; private int age; // 添加默认构造函数和getter/setter方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } } ``` 在这个例子,我们使用Vue.js在前端创建了一个简单的表单,包含了一个文本输入框用于输入姓名和年龄,并有一个按钮用于发送数据。我们使用`v-model`指令将输入框的值绑定到Vue实例的`name`和`age`属性上。 在Vue实例的`sendData`方法,我们使用Axios库发送POST请求到后端的`/backend.php`接口,并将`name`和`age`作为请求体发送。在成功响应后,我们将后端返回的响应数据赋值给Vue实例的`response`属性,用于显示在页面上。 后端使用Spring Boot框架编写了一个简单的Java应用程序。我们创建了一个RESTful接口`/backend.php`,通过`@PostMapping`注解来处理POST请求。请求体的数据会自动映射到`RequestData`类的属性。在处理数据时,我们将姓名和年龄拼接成字符串作为响应返回。 请注意,需要安装Vue.js和Axios库,以及配置好Java开发环境和Spring Boot依赖。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值