如何将值从JSP动态传递到Struts Java代码 [英] How to pass value from JSP to Struts java code dynamically

查看:92
本文介绍了如何将值从JSP动态传递到Struts Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Struts.我有一个JSP登录页面,用户可以通过该页面提交他的数据.之后,如果有人要搜索他的数据,他必须输入他的id号.我希望此id是动态输入的.此id否将被传递到Java页面.我正在使用JDBC.谁能帮我编写查询和设置字符串函数.

I am working on Struts. I have a JSP login page through which user will submit his data. After that if anyone want to search his data he have to enter his id no. I want this id to be entered dynamically. This id no will be passed to a Java page. I am using JDBC. Can any one help me how to write the query and set string function.

这是我的Java代码:

Here is my Java code:

// package com.javatpoint;  
import java.sql.*;  
import java.util.ArrayList;  

public class Display {  
ArrayList<User> list=new ArrayList<User>();  
  User user= new User();
public ArrayList<User> getList() {  
    return list;  
}  
public void setList(ArrayList<User> list) {  
    this.list = list;  
}  
public String execute(){  
 try{  
     Class.forName("com.mysql.jdbc.Driver");
       Connection con=DriverManager.getConnection(  
                "jdbc:mysql://localhost:3306/test","root","Ericsson@123");  

  PreparedStatement ps=con.prepareStatement("select * from  STRUTSUSER where id=?");
  ps.setInt(0,user.getId());  
  ResultSet rs=ps.executeQuery();  

  while(rs.next()){  
   User user=new User();  
   user.setId(rs.getInt(1));  
   user.setName(rs.getString(2));  
   user.setPassword(rs.getString(3));  
   user.setEmail(rs.getString(4));  
   list.add(user);  
  } 


 }catch(Exception e){e.printStackTrace();}  

 return "succes";
}  
}  

struts.xml:

struts.xml:

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD   
     Struts Configuration 2.1//EN"   
 "http://struts.apache.org/dtds/struts-2.0.dtd">  
<struts>  

 <package name="anbc" namespace="/" extends="struts-default">  
  <action name="display" class="com.javatpoint.Display">  
    <result name="success">display.jsp</result>  
  </action>  
 </package> 
 <package name="abc" namespace="/" extends="struts-default">  
  <action name="login" class="com.javatpoint.Register">  
    <result name="success">welcome.jsp</result>  
  </action>  
 </package>  

</struts>   

推荐答案

如果对模型使用User对象,但不使用模型驱动的动作,则要使用id吸引用户,则应使用user.id请求中的参数.您还应该为模型使用吸气剂

If you use User object for the model, but not use model driven action, then to get user by the id you should use user.id parameter in the request. You should also use a getter for the model

public User getUser(){
  return user;
}

调用操作时,它将参数设置为用户对象.现在将其设置为查询中的参数

When the action is called it will set the parameter to the user object. Now to set it as parameter in the query

ps.setInt(1, user.getId()); //parameters are counted from 1..n

查看使用 PreparedStatement 的详细信息.

您应该返回Action.SUCCESS结果,并且无需命名结果"success",因为它是默认名称.

You should return Action.SUCCESS result, and there's no need to name a result "success" because it's default name.

return Action.SUCCESS;

如果将用户ID作为参数传递,则列表中不会得到多个对象,但是要在JSP中绑定列表,可以使用iterator标记

If you are passing user id as a parameter then you won't get more than one object in the list, but to bind a list in JSP you can use iterator tag

<s:iterator value="list">
  ID: <s:property value="id"/><br>
  Name: <s:property value="name"/><br>
  Email: <s:property value="email"/><br>      
</s:iterator> 

属性在User对象中应具有相应的吸气剂.

The properties should have corresponding getters in the User object.

实际上,加载类和打开连接(您忘记关闭)是不好的.要获取Connection对象,可以配置和使用数据源.有关示例,请参见如何通过JDBC集成JSF 如何使用Struts 2在Apache Tomcat的web.xml中创建MySQL数据库连接.

Loading classes and opening connection (that you forgot to close) in action is bad. To get a Connection object you can configure and use a data source. See for examples How JSF is inegrated with JDBC and How to create MySQL database connection in web.xml of Apache Tomcat using Struts 2.

这篇关于如何将值从JSP动态传递到Struts Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆