2016-07-05 348 views
-1

我有一個包含圖像的jsp頁面,當我點擊theat圖像時,它應該轉到其他jsp頁面。如何在struts2中執行此操作。在添加圖像動作之前它工作正常。當我點擊圖像時struts動作

的Login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
    <%@ 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=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<s:form action="login" method="post"> 
<s:textfield name="id" label="Employee Id"></s:textfield> 
<s:textfield name="name" label="Employee Name"></s:textfield> 
<s:submit value="save"></s:submit> 
</s:form> 
</body> 
</html> 

LoginAction.java:

package login; 

public class LoginAction { 

    private int id; 
    private String name; 

    public int getId() { 
     return id; 
    } 

    public String getName() { 
    return name; 
    } 

    public String execute(){ 
     return "success"; 
} 
} 

struts.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0.//EN" 
    "http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
<constant name="struts.devMode" value="true" /> 
    <package name="login" extends="struts-default" namespace="/" > 
    <default-action-ref name="upload" /> 



    <action name="login" class="login.LoginAction" method="execute"> 
<result name="success">products.jsp</result> 
</action> 

<action name="click" class="login.ItemAction" method="execute"> 
<result name="success">productdetails.jsp</result> 
</action> 

    </package> 
</struts> 

web.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 
    <display-name>dhanya</display-name> 


    <filter> 
     <filter-name>struts2</filter-name> 

    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 


    </filter> 

    <filter-mapping> 
     <filter-name>struts2</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 



    <welcome-file-list> 
    <welcome-file>Login.jsp</welcome-file> 
    </welcome-file-list> 
    </web-app> 

Products.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
    <%@ 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=ISO-8859-1"> 
<title>product images</title> 

</head> 
<div align="center" style="white-space: nowrap;word-wrap:break-word;"> 
    <%@ include file="Header.jsp" %> 

    </div> 
<body> 

<a href="<s:url action="click"/>"></a><img src="images/bp.jpg" class="tooltip" title="BP monitor" id="BP monitor"> </a> 
<a href="<s:url action="click"/>">"><img src="images/sethescope.jpg" class="tooltip" title="sethescope" id="sethescope"></a> 

    </body> 
    <div align="center" style="white-space: nowrap;word-wrap:break-word;"> 
    <%@ include file="Footer.jsp" %> 

    </div> 

</html> 

ItemAction.java

package login; 

public class ItemAction { 
    public String execute(){ 
      return "success"; 
    } 

} 

enter image description here

回答

0

如果你只是想用錨相關聯的圖像,你可以如下做到這一點:

<s:url var='imageUrl' action='yourActionName'/> 
<s:a href='%{#imageUrl}'><img src='<s:url value="images/yourimage.jpg"/>'/></s:a> 

您會注意到我構建了一個指定的URL,引用了指定的URL i n Struts2定位標記的href屬性,並使用內聯URL來引用要顯示的圖像的正確的基於上下文的路徑。

但是,對於您的問題,您看起來像在products.jsp中包含的第一個圖像鏈接中有一個流浪的</a>

+0

我需要它像,我在products.jsp頁兩幅圖像,當我點擊一個圖像,它應該去其他JSP page.how動作調用在struts2 –

+0

有人可以幫我做到這一點,這是非常迫切的 –

0

首先,您首先在strtus.xml文件中定義動作,然後展示該動作類,然後我們可以使用這種方式調用動作。

Products.jsp

<a href="<s:url action="cancel.action" />"><img src="image/1.png" class="tooltip" height="42" width="42" title="BP monitor" id="BP monitor" > </a> 

<a href="<s:url action="hello.action"/>"><img src="image/2.jpg" height="42" width="42" class="tooltip" title="sethescope" id="sethescope"></a> 

和strtus.xml

<action name="cancel" 
     class="com.test.CancelAction" 
     method="execute"> 
     <result name="success">/Cancel.jsp</result> 
    </action> 

    <action name="hello" 
     class="com.test.ApproveAction" 
     method="execute"> 
     <result name="success">/Approve.jsp</result> 
    </action> 

和定義類,這是我對XML文件中定義與回報Sucess消息和檢查。它將肯定工作。對不起,我爲了測試目的而拍攝了一些其他圖片。感謝和任何疑問在這裏問。

變化<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>在strtus.xml

+0

:仍然不工作。我收到以下錯誤:嚴重:例外開始篩選器struts2 無法加載配置。 - action - file:/ C:/Users/RajD/Documents/java/apache-tomcat-8.0.36-windows-x64/apache-tomcat-8.0.36/webapps/dhanya/WEB-INF/classes/struts.xml :19:26 –

+0

@DhanyaRaj我已更新了Strtus.xml中更改過濾器URL的答案,請在我的帖子中查看更新後的URL。 –

+0

@DhanyaRaj你試過了嗎?與更新的答案 –

相關問題