綜合知識

當前位置 /首頁/綜合知識 > /列表

struts的action怎麼寫

1. 關於struts2的 action怎麼寫

額 看不懂你説的哦

struts的action怎麼寫

不過struts2中的action最普遍要繼承ActionSupport這個類,而你們老師繼承ServletRequestAware類反正我們用過,然後就是定義實體類,和藉口的方法。在spring中注入

struts2中的方法以public String +方法名(){return "111";}來進行操作 return 裏面的字符串要和你struts2配置中的那個<result name="111">;/頁面的位置</result>;中的name屬性相同,你在登錄頁面的form中調用你寫的“方法名”就進入方法中如果成功返回“111”就會進入你所設置的頁面位置。

2. List 查詢struts1的Action怎麼寫

struts.xml: &lt;action path="/user" scope="request" parameter="f"

type="com.manage.struts.action.sysmanage.UserManageAction"&gt;

&lt;forward name="success" path="" /&gt;

&lt;/action&gt;

action : extends DispatchAction

public ActionForward loginOut(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws IOException {

request.getSession().removeAttribute("userSession");

request.getSession().invalidate();

HashMap&lt;String, Object&gt; jsonMap = new HashMap&lt;String, Object&gt;();

jsonMap.put("success", true);

jsonMap.put("message", "退出登錄成功");

JSONObject json = new JSONObject();

json.putAll(jsonMap);

Writer writer = response.getWriter();

writer.write(json.toString());

writer.flush();

return mapping.findForward("");

}

3. struts2中一個action中實現多個功能應該怎麼寫

可以在action中寫多個你需要的方法..然後在struts.xml中為這個action類配置多個你可以給這些起不同名字,然後用method屬性制定要執行哪個方法。

例如:/login.jsp/Login.jsp/welcome.jsp例如上面的配置就是為LoginAction這個action類配置了2個而起了不同的名字其中第一個指明瞭method,用户請求forwardLogin時就會調用LoginAction中的forward()方法第二個沒有明瞭method,用户請求login時就會調用LoginAction中默認的execute()方法。

4. struts2中action裏面怎麼寫一個方法直接查詢數據庫數據,

先寫的DAO:public List<FileModel> findAll()

{

Connection con = null ;

PreparedStatement ps = null ;

FileModel file = null ;

ResultSet rs = null;

List<FileModel> set = null ;

try

{

con = DBconnection.getConnection();

String sql = "select * from file ;

ps = con.prepareStatement(sql);

set = new LinkedList<FileModel>();

rs = ps.executeQuery() ;

while(rs.next())

{

file = new FileModel();

file.setFilepath(rs.getString(1));

file.setFiletime(rs.getTimestamp(2));

file.setFileintroduce(rs.getString(3));

file.setFilediscuss(rs.getString(4));

file.setFilescore(rs.getFloat(5));

file.setFiletype(rs.getString(6));

file.setDirection(rs.getString(7));

file.setFileid(rs.getInt(8));

file.setFilename(rs.getString(9));

set.add(file);

}

}

catch(SQLException e)

{

throw new RuntimeException(e.getMessage(),e);

}

finally

{

DBconnection.free(rs, ps, con);

}

return set;

}

在action中調用DAO:

public class FindAction extends ActionSupport {

private Dao dao = new Dao();

@Override

public String execute() throws Exception {

// TODO Auto-generated method stub

LinkedList<FileModel> modelSet = (LinkedList<FileModel>) dao.findAll();

if (modelSet!=null){

System.out.println(modelSet);

ActionContext.getContext().getSession().put("msg", modelSet);

return SUCCESS;}

else

return ERROR;

}

}

TAG標籤:struts action #