标题:
jsp接收action的值的方法总结(2)
[打印本页]
作者:
look_w
时间:
2019-4-20 14:46
标题:
jsp接收action的值的方法总结(2)
完整代码例子
TestAction.java
package action;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport{
/**
* action调用样例
*/
private static final long serialVersionUID = -3544701251834366894L;
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute(){
System.out.println("testAction runing!");
name="我是action中通过get传递的值";
/* HttpServletRequest request = ServletActionContext.getRequest ();
request.setAttribute("SR","我是ServletActionContext作容器Request传值");
ServletContext application = ServletActionContext.getServletContext ();
application.setAttribute("SA","我是ServletActionContext作容器application传值");
HttpSession session = ServletActionContext.getRequest ().getSession();
session.setAttribute("SS","我是ServletActionContext作容器session传值");*/
ActionContext context= ActionContext.getContext();
Map request = (Map)context.get("request");
request.put("AR","我是ActionContext作容器Request传值");
Map application = (Map)context.getApplication();
application.put("AA","我是ActionContext作容器application传值");
Map session = (Map)context.getSession();
session.put("AS","我是ActionContext作容器session传值");
return SUCCESS;
}
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<s:property value="name"/><BR/>
<s:property value="#request.SR"/><BR/>
<s:property value="#application.SA"/><BR/>
<s:property value="#session.SS"/><BR/>
<s:property value="#request.AR"/><BR/>
<s:property value="#application.AA"/><BR/>
<s:property value="#session.AS"/>
</body>
</html>
spring action.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/s ... ing-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
">
<bean id="testAction" class="action.TestAction">
</bean>
<bean id="login" class="action.LoginAction">
</bean>
</beans>
struts action.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="getIp" extends="json-default" namespace="/getIp">
<action name="testJson" class="testAction">
<result type="json"></result>
</action>
<action name="testJsp" class="testAction">
<result name="success">/index.jsp</result>
</action>
<action name="login" class="login">
<result type="json"></result>
</action>
</package>
</struts>
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0