新人求救,SSH如何在二次跳转后的Action中获取参数?

嘻唔徐 2019-05-09 10:29:02
问题描述:
第一个页面是个jsp登录框,有用户名和密码可以输入


接下来在第一个Action中:InfoLoginAction 对输入的用户名和密码进行查询数据库比对,如果用户存在,并且该用户的类型为“维护人员”,那么跳转到第二个Action中InfoQueryAction,通过输入的登录名,获取到该用户的部分信息,呈现在第二个jsp中shenqing.jsp中。



现在问题是,我在第一个jsp页面登录页面中,输入了符合要求的用户名和密码 ,第一个Action是可以获取到我的输入值的,在跳转到第二个Action中的时候,我该怎么获取到这个输入的用户名呢??

代码如下:
第一个jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>

</head>
<body>
<form id="login" action="infoLogin" method="post" namespace="/">
<h1>登录</h1>
<fieldset id="inputs">
<input name="info.dl_name" type="text" required="required"
placeholder="Username" tabindex="1" autofocus>
<input name="info.dl_pass" type="password" required="required"
placeholder="Password" tabindex="2">
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" value="Log In">
<a href="./update.jsp">Forgot your password?</a>
<a href="./1_login.jsp">Register</a>
</fieldset>
</form>
</body>
</html>


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">

<struts>

<constant name="struts.devMode" value="false" />

<constant name="struts.objectFactory" value="spring"></constant>

<package name="default" namespace="/" extends="struts-default">
<!-- class属性值是spring定义的Bean,当执行execute()返回success时,控制转到另一个动作memberQuery -->
<action name="infoRegister" class="infoRegisterAction">
<result name="success" type="redirectAction">infoQuery</result>
</action>

<action name="infoLogin" class="infoLoginAction">
<result name="weihu" type="redirectAction">
<param name="actionName">infoQuery</param>
<!--<param name="info">${info}</param> -->
</result>
<result name="zhiban">register.jsp</result>
<result name="ywsp">register.jsp</result>
<result name="kfsp">register.jsp</result>
<result name="err">register.jsp</result>
</action>

<action name="infoQuery" class="infoQueryAction">
<result name="success">shenqing.jsp</result>
</action>

<action name="infoDelete" class="infoDeleteAction">
<result name="success" type="redirectAction">infoQuery</result>
</action>

<action name="infoShow" class="infoUpdateAction" method="showInfo">
<result name="success">update.jsp</result>
</action>

<action name="infoUpdate" class="infoUpdateAction">
<result name="success" type="redirectAction">infoQuery</result>
</action>
</package>
</struts>


appliactionContext.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<!-- spring 框架提供了多种数据源类,可以使用spring提供的DriverManagerDataSource类还可以使用第三方数据源,
如C3P0的ComboPooledDataSource数据源类,注意相应的两个jar包c3p0-0.9.2.1.jar mchange-commons-java-0.2.3.4.jar -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)(SERVER=DEDICATED)))" />
<property name="username" value="admin" />
<property name="password" value="admin" />
<!-- <property name="maxPoolSize" value="40" />
<property name="minPoolSize" value="1" />
<property name="initialPoolSize" value="2" />
<property name="maxIdleTime" value="20" /> -->
</bean>

<!-- Hibernate4已经完全实现自己的事物管理,所以spring4不提供HibernateDaoSupport和HibernateTemplete的支持
可以在应用程序的spring上下文中,像配置其他bean那样来配置HibernateSession工厂。 如果要使用XML文件定义对象与数据库之间的映射,则需要在spring中配置LocalSessionFactoryBean。
hibernateProperties属性配置了Hibernate如何进行操作的细节。 "hibernate.current_session_context_class"是为当前Session提供一个策略,
Session由org.springframework.orm.hibernate4.SpringSessionContext.currentSession得到。
将sessionFactory注入到其他Bean中,如注入到DAO组件中,使其获得SessionFactory的引用后,就可以实现对数据库的访问。 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/entity/Info.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate4.SpringSessionContext
</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>

<bean id="infoDao" class="com.dao.InfoDAOImpl">
<!-- 构造方法注入会话工厂组件sessionFactory -->
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>

<bean id="infoService" class="com.service.InfoServiceImpl">
<!-- 设值注入DAO组件 -->
<property name="infoDao" ref="infoDao"></property>
</bean>

<bean id="infoRegisterAction" class="com.action.InfoRegisterAction">
<!-- 设值注入业务逻辑组件 -->
<property name="infoService" ref="infoService"></property>
</bean>

<bean id="infoLoginAction" class="com.action.InfoLoginAction">
<property name="infoService" ref="infoService"></property>
</bean>

<bean id="infoQueryAction" class="com.action.InfoQueryAction">
<property name="infoService" ref="infoService"></property>
</bean>

<bean id="infoDeleteAction" class="com.action.InfoDeleteAction">
<property name="infoService" ref="infoService"></property>
</bean>

<bean id="infoUpdateAction" class="com.action.InfoUpdateAction">
<property name="infoService" ref="infoService"></property>
</bean>
</beans>


第一个Action---InfoLoginAction文件:
package com.action;

import com.entity.Info;
import com.opensymphony.xwork2.ActionSupport;
import com.service.InfoService;

public class InfoLoginAction extends ActionSupport {

private static final long serialVersionUID = 1L;
private Info info;
private InfoService infoService;

public Info getInfo() {
return info;
}

public void setInfo(Info info) {
this.info = info;
}

// 注入业务逻辑组件
public void setInfoService(InfoService infoService) {
this.infoService = infoService;
}

public String execute() {
Info mb = infoService.findByName(info.getDl_name(), info.getDl_pass());
if (mb != null && mb.getLeibie().equals("维护人员"))
return "weihu";
else if (mb != null && mb.getLeibie().equals("值班人员"))
return "zhiban";
else if (mb != null && mb.getLeibie().equals("运维审批"))
return "ywsp";
else if (mb != null && mb.getLeibie().equals("开发审批"))
return "kfsp";
else
return "err";
}
}


第二个Action--InfoQueryAction

package com.action;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.entity.Info;
import com.opensymphony.xwork2.ActionSupport;
import com.service.InfoService;

public class InfoQueryAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private InfoService infoService;

private Info info;

public Info getInfo() {
return info;
}

public void setInfo(Info info) {
this.info = info;
}

public void setInfoService(InfoService infoService) {
this.infoService = infoService;
}

public String execute() {
List<Info> list = infoService.findOne("luchang");
ServletActionContext.getRequest().setAttribute("infoList", list);
return SUCCESS;
}
}


第二个jsp申请页面:shenqing.jsp (内容太多了,节选主要的)


<form action="">
<table width="1000px" cellspacing="0" align="center">
<thead>
<tr>
<td colspan="4"><caption>生产操作间进出申请单</caption></td>
</tr>
<!-- <tr>
<td colspan="3" width="85%" style="text-align: right;"><sub>编号:</sub></td>
<td width="15%"><input type="text" name="bianhao"
value="" disabled="disabled" size=35 /></td>
</tr> -->
</thead>
</table>

<table width="1000px" border="3" cellspacing="0" cellpadding="0"
align="center">
<tbody>

<tr>
<td height="30" colspan="4" style="background-color: #AAA;">以下为系统自动填充,无需填写,可根据实际需要调整进入和离开的时间</td>
</tr>
<s:iterator value="#request.infoList" id="info">
<tr>
<td width="200px" height="25">申请单编号</td>
<td width="300px"><input type="text" name="id"
value="<%=number%>" disabled="disabled" size=38 /></td>
<td width="200px">申请日期</td>
<td width="300px"><input type="text" name="renyan"
value="<%=dateT%>" disabled="disabled" size=38 /></td>
</tr>
<tr>
<td height="25">人员姓名</td>
<td><!-- <input type="text" name="riqi" value="#info.xingming"
disabled="disabled" size=38 /> -->
<s:property value="#info.xingming" /></td>
<td>联系方式</td>
<td><input type="text" name="renyan" value=""
disabled="disabled" size=38 /></td>
</tr>
...全文
188 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dkodak 2019-05-10
  • 打赏
  • 举报
回复
放session去取方便的多
maradona1984 2019-05-09
  • 打赏
  • 举报
回复
重定向的话,那就把参数拼到url上就行了,转发的话参数不会丢失

当然我是觉得你这种方案是极为不妥的,登录那就做登录的逻辑,登录成功把用户信息丢到session,你重定向之后从session取出用户信息即可,为何还要从请求中去拿用户名?这是一种不安全的操作,我在你重定向的url上加上用户名参数就可以访问这个页面了,这就相当于绕开了登录操作
maradona1984 2019-05-09
  • 打赏
  • 举报
回复
url?参数名=参数值
是否看懂?
嘻唔徐 2019-05-09
  • 打赏
  • 举报
回复
引用 1 楼 maradona1984 的回复:
重定向的话,那就把参数拼到url上就行了,转发的话参数不会丢失 当然我是觉得你这种方案是极为不妥的,登录那就做登录的逻辑,登录成功把用户信息丢到session,你重定向之后从session取出用户信息即可,为何还要从请求中去拿用户名?这是一种不安全的操作,我在你重定向的url上加上用户名参数就可以访问这个页面了,这就相当于绕开了登录操作
暂时不考虑安全方面的问题,具体代码咋写啊? 在哪个url里拼接? 怎么拼接,怎么取?新手啊

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧