jsp struts action2 jsp页面如何获得action中的值

struts2中如何获取request response 和session等对象-SSH教程-ab蓝学网
struts2中如何获取request response 和session等对象
简介:java教程|struts2获取request,struts2获取respons,struts2获取session2种实现方法:1非IoC方式要获得这几个对象,关键Strut
struts2获取request,struts2获取respons,struts2获取session&2种实现方法:
1 非IoC方式 &&& 要获得这几个对象,关键Struts 2.0中com.opensymphony.xwork2.ActionContext类。我们可以通过它的静态方法getContext()获取当前Action的上下文对象。 ActionContext.getContext()code]另外,org.apache.struts2.ServletActionContext作为辅助类(Helper Class),可以帮助您快捷地获得这几个对象。
HttpServletRequest&request&=&ServletActionContext.getRequest();& &&
HttpServletResponse&response&=&ServletActionContext.getResponse();& &&
HttpSession&session&=&request.getSession();&&
&&& 如果你只是想访问session的属性(Attribute),你也可以通过ActionContext.getContext().getSession()获取或添加session范围(Scoped)的对象。
import&javax.servlet.http.HttpServletR &&
import&javax.servlet.http.HttpServletR &&
import&javax.servlet.http.HttpS&&&
import&org.apache.struts2.ServletActionC&&&
import&com.opensymphony.xwork2.ActionC&import&com.opensymphony.xwork2.ActionS &&
&&public&class&NonIocServlet&extends&ActionSupport&{ &&
&&&&private&String&&&&
&&&&public&String&getMessage()&{ &&
&&&&&&&&return&&&&&&&&& &&
&&&&@Override&&
&&&&public&String&execute()&{&&&& &&
&&&&&&&&ActionContext.getContext().getSession().put(&msg&,&&Hello&World&from&Session!&);&&&
&&&&&&&&HttpServletRequest&request&=&ServletActionContext.getRequest(); &&
&&&&&&&&HttpServletResponse&response&=&ServletActionContext.getResponse();&&&&&&&& &&
&&&&&&&&HttpSession&session&=&request.getSession();&&&&&&&&&&&&&&
&&&&&&&&StringBuffer&sb&=new&StringBuffer(&Message&from&request:&&); &&
&&&&&&&&sb.append(request.getParameter(&msg&)); &&
&&&&&&&&sb.append(&&br&Response&Buffer&Size:&&); &&
&&&&&&&&sb.append(response.getBufferSize()); &&
&&&&&&&&sb.append(&&br&Session&ID:&&); &&
&&&&&&&&sb.append(session.getId());&&&
&&&&&&&&message&=&sb.toString(); &&
&&&&&&&&return&SUCCESS; &&
2 IoC方式 要使用IoC方式,我们首先要告诉IoC容器(Container)想取得某个对象的意愿,通过实现相应的接口做到这点。
import&java.util.M &&
import&javax.servlet.http.HttpServletR &&
import&javax.servlet.http.HttpServletR &&
import&javax.servlet.http.HttpS &&
import&org.apache.struts2.interceptor.ServletRequestA &&
import&org.apache.struts2.interceptor.ServletResponseA &&
import&org.apache.struts2.interceptor.SessionA &&
import&com.opensymphony.xwork2.ActionC &&
import&com.opensymphony.xwork2.ActionS &&
&publicclass&IoCServlet&extends&ActionSupport&implements&SessionAware,&ServletRequestAware,&ServletResponseAware&{ &&
&&&&private&String& &&
&&&&private&Map& &&
&&&&private&HttpServletRequest& &&
&&&&private&HttpServletResponse&&&&& &&
&&&&public&String&getMessage()&{ &&
&&&&&&&&return&&&&&&&&& &&
&publicvoid&setSession(Map&att)&{ &&
&&&&&&&&this.att&=& &&
&&&&publicvoid&setServletRequest(HttpServletRequest&request)&{ &&
&&&&&&&&this.request&=& &&
&&&publicvoid&setServletResponse(HttpServletResponse&response)&{ &&
&&&&&&&&this.response&=& &&
&&&&@Override&&
&&&&public&String&execute()&{&&&&&&&& &&
&&&&&&&&att.put(&msg&,&&Hello&World&from&Session!&); &&
&&&&&&&&HttpSession&session&=&request.getSession(); &&
&&&&&&&&StringBuffer&sb&=new&StringBuffer(&Message&from&request:&&); &&
&&&&&&&&sb.append(request.getParameter(&msg&)); &&
&&&&&&&&sb.append(&&br&Response&Buffer&Size:&&); &&
&&&&&&&&sb.append(response.getBufferSize()); &&
&&&&&&&&sb.append(&&br&Session&ID:&&); &&
&&&&&&&&sb.append(session.getId()); &&
&&&&&&&&message&=&sb.toString(); &&
&&&&&&&&return&SUCCESS; &&
上一编:下一编:
喜欢猜你喜欢的内容
大家感兴趣的内容
1 2 3 4 5 6 7 8 9 10
最近更新的内容
1 2 3 4 5 6 7 8 9 10
本月热门的内容
1 2 3 4 5 6 7 8 9 10对于如何把struts2的action中的值传到jsp页面中,主要的方法有2种:
使用转发视图利用request域中储存所需的值
使用重定向时存储数据进入session使其在jsp中可以获得
下面,让我们分先后的讲解这2种方法!
一、转发到jsp:
准备步骤:
jsp页面随时准备使用获取request中的值,
action中的值分2种,一种是在方法中诞生的,作用域也是方法内;一种是定义为类成员(并带有get,set或者为public),此种方式只需赋值。
&&&&&2.操作流程:
对于action的类属性,直接为其赋值(例如 message="Test";),然后在jsp页面中使用取出这个值(${message },直接以类属性名称取值即可,el默认的取值域就是request,struts2会将action的所有带有get,set(这两个方法必须同时有)的属性自动的放入request域中【当然你也可以使用struts的标签库,不推荐】)
对于action中方法中产生的数据,又不想为其在action类中定义对应的成员,我们可以手动的将其存放进入request域中,在struts2中,有2种方式可以将数据存入request中。
        (1)一种是通过ActionContext.getContext().put("message",message);直接放入将数据request域中,不获取request本身,获取其
           中的Map;
        (2)另一种是使用ServletActionContext.getRequest().setAttribute("message",message);直接取得,再使用
&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 的方法setAttribute()方法存入数据!将数据存入request中后,jsp中取值并无差别(同上)!
二、重定向到jsp:
准备步骤:同上!不同处是不管要存入的数据是不是类的属性,我们都必须在方法中进行处理才能将数据存入session,而不能依靠struts2自动帮我们搞定!
操作流程:
不管数据是哪里诞生的,得到数据后!比如为message;同上的第二种情况:也有2中方式将数据存入到session中:
(1)在方法中通过ActionContext().getContext().getSession().put("message",message);首先获取session中的map,再使用的方法put()存入数据
(2)在方法中通过ServletActionContext().getRequest().getSession().setAttribute("message",message);首先获取,再使用的setAttribute方法将数据存入session域。
最后,不管那种方式将数据存入到session中,我们都可在jsp页面中获取session中的值,例如${sessionScope.message }
三、总结:
不论如何,总是将数据存入tomcat提供的几个存数据的域中(request,session,application),然后在页面通过获取即可!
阅读(...) 评论()17:02 提问
SSH中Struts2获取不到jsp页面中值,求大神指点
ssh框架整合,添加员工信息;
员工,Emp:
package com.blue.
public class Emp {
//id,数据库中为标识列
private int empId;
private String empN
private int empA
private String empS
public int getEmpId() {
return empId;
public void setEmpId(int empId) {
this.empId = empId;
public String getEmpName() {
return empN
public void setEmpName(String empName) {
this.empName = empN
public int getEmpAge() {
return empA
public void setEmpAge(int empAge) {
this.empAge = empA
public String getEmpSex() {
return empS
public void setEmpSex(String empSex) {
this.empSex = empS
jsp页面,form:
&form action="empLogin" method="post"&
&table width="300px" height="150px" border="1px"&
&td width="100px"&姓名:&/td&
&td&&input type="text" name="emp.empName"/&&/td&
&td&年龄:&/td&
&td&&input type="text" name="emp.empAge"/&&/td&
&td&性别:&/td&
&td&&input type="text" name="emp.empSex"/&&/td&
&td colspan="2"&&input type="submit" value="保存"&&/td&
package com.blue.
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
import com.blue.entity.E
import com.blue.service.EmpS
import com.opensymphony.xwork2.ActionS
public class EmpAction
extends ActionSupport{
public String result(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
EmpService empService= (EmpService) context.getBean("empService");
Emp emp = (Emp) context.getBean("emp");
System.out.println("id:"+emp.getEmpId()+"
name:"+emp.getEmpName()+"
age:"+emp.getEmpAge()+"
sex:"+emp.getEmpSex());
int num = empService.save(emp);
if(num == -1){
return "empty";
}else if(num & 0){
//添加成功
return SUCCESS;
return ERROR;
控制台输出:
spring配置文件(beans.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"&
&bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&
&property name="configLocation"&
classpath:hibernate.cfg.xml
&/property&
&bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&
&property name="sessionFactory" ref="sessionFactory"&&/property&
&bean id="emp" class="com.blue.entity.Emp"&&/bean&
&bean id="empDao" class="com.blue.dao.impl.EmpDaoImpl"&
&property name="hibernateTemplate" ref="hibernateTemplate"&&/property&
&bean id="empService" class="com.blue.service.impl.EmpServiceImpl"&
&property name="empDao" ref="empDao"&&/property&
&bean id="empAction" class="com.blue.action.EmpAction" scope="prototype"&&/bean&
struts.xml:
&?xml version="1.0" encoding="UTF-8"?&
&!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"&
&constant name="struts.objectFactory" value="spring"&&/constant&
&package name="default" namespace="/" extends="struts-default"&
&action name="empLogin" class="com.blue.action.EmpAction" method="result"&
&result name="success"&welcome.jsp&/result&
&result name="error"&error.jsp&/result&
&!-- 空值 --&
&result name="empty"&empty.jsp&/result&
&/package&
取不到页面值,求大神指点
按赞数排序
EmpAction 类不需要实例化Emp对象,应该是
public void setEmp(Emp emp){
publilc String getEmp(){
你可以看看域模型(DomainModel),再看看驱动模型,看看他们的区别
主要是这两个模型你弄混了
问题已解决,同时感谢楼上热心回答Struts2中的链接标签 &s:url&和&s:a&---在action中获取jsp表单提交的参数 - 大地 - ITeye博客
博客分类:
普通链接
Web程序中最普通的应用是链接到其他页面,下面看Welcome.jsp。
&%@ page contentType="text/ charset=UTF-8" %&
&%@ taglib prefix="s" uri="/struts-tags" %&
&html&
&head&
&&& &title&Welcome&/title&
&&& &link href="&s:url value="/css/tutorial.css"/&" rel="stylesheet"
&&&&&&&&& type="text/css"/&
&/head&
&body&
&h3&Commands&/h3&
&ul&
&&& &li&&a href="&s:url action="Login_input"/&"&Sign On&/a&&/li&
&&& &li&&a href="&s:url action="Register"/&"&Register&/a&&/li&
&/ul&
&/body&
&/html&
1.1说明
1.&%@ taglib prefix="s" uri="/struts-tags" %&
此句表示导入struts标签,并以s为前缀。即以s为前缀的标签均来自struts标签库。
2.&link href="&s:url value="/css/tutorial.css"/&" rel="stylesheet" type="text/css"/&
此句表示利用url标签导入一个路径,链接到一个文件,注意此路径为项目下的绝对路径。
3.&a href="&s:url action="Login_input"/&"&Sign On&/a&
此句表示利用url标签链接到一个action。
1.2注册action
我们在struts.xml中注册一个action来显示welcome.jsp。
&action name="Welcome"&
&&&&&& &result&/example/Welcome.jsp&/result&
&/action&
注意此action注册在package example下,所以在地址栏中敲入http://localhost:8080/StrutsHelloWorld/example/Welcome.action(StrutsHelloWorld是project名),会导向到Welcome.jsp。
2.使用通配符
对于上面的action注册,我们也可以用下面的语句代替。
&action name="*"&
&&&&&& &result&/example/{1}.jsp&/result&
&/action&
此句的意思是,如果在没有找到匹配的action名称的情况下,默认调用action名称.jsp。第一句中星号指任意,而第二句中{1}指代第一句中星号指代的内容。
&&& 举个例子,如果在地址栏中敲入http://localhost:8080/StrutsHelloWorld/example/1.action,则系统查找struts.xml,发现没有name为1的action,即最后调用name为星号的这个action,根据此action,将输出/example/1.jsp。
或者读者可以直接点击Welcome.jsp中的两个超链接,系统将会报错找不到Login_input.jsp和Register.jsp。因为这两个action还没有注册,也没有相应的jsp文件。
3.带参数的链接
超链接后面带有参数大家不会陌生,诸如/?language=ch。这个链接后面带有一个language参数,其值为ch。你可以通过request.getParameter(“language”)找到参数值。下面演示在struts2中如何设置带参数的链接。看HelloWorld.jsp。
&%@ taglib prefix="s" uri="/struts-tags"%&
&html&
&head&
&title&Hello World!&/title&
&/head&
&body&
&h2&&s:property value="message" /&&/h2&
&h3&Languages&/h3&
&ul&
&&&&&& &li&
&&&&&& &s:url id="url" action="HelloWorld"&
&&&&&&&&&&&&& &s:param name="request_locale"&en&/s:param&
&&&&&& &/s:url&
&&&&&& &s:a href="%{url}"&English&/s:a&
&&&&&& &/li&
&&&&&& &li&
&&&&&& &s:url id="url" action="HelloWorld"&
&&&&&&&&&&&&& &s:param name="request_locale"&es&/s:param&
&&&&&& &/s:url&
&&&&&& &s:a href="%{url}"&Espanol&/s:a&
&&&&&& &/li&
&/ul&
&/body&
&/html&
3.1说明
1.&s:url id="url" action="HelloWorld"&
&&&&&&&&&&&&& &s:param name="request_locale"&en&/s:param&
&/s:url&
此段表示设置一个url标签指向名为HelloWorld的action,此标签带一个id取名为url,后面会用到。带一个参数request_locale,其值为en。
2.&s:a href="%{url}"&English&/s:a&
此句用到了struts2的超链接标签,连接的地址即为1中url,点击English,发出的信息为:http://localhost:8080/StrutsHelloWorld/example/HelloWorld.action?request_locale=en
3.2注册action到struts.xml
&struts&
&&&&&& &package name="example" namespace="/example"
&&&&&&&&&&&&& extends="struts-default"&
&&&&&&&&&&&&& &action name="HelloWorld" &
&&&&&&&&&&&&&&&&&&&& &result&/example/HelloWorld.jsp&/result&
&&&&&&&&&&&&& &/action&
浏览 14329
浏览: 111446 次
来自: 邯郸
楼主,你这种方方式创建在IE8下post不起作用啊,有没有更好 ...
正确的和错误的有什么区别?
lishirong 写道据说是Hibernate和Spring ...
据说是Hibernate和Spring的asm包的版本冲突,所 ...
怎么给Queue中的元素排序?

我要回帖

更多关于 struts2 jsp页面跳转 的文章

 

随机推荐