点击图片时struts动作

时间:2016-07-05 07:12:18

标签: jsp struts2

我有一个包含图像的jsp页面,当我点击该图像时,它应该转到其他jsp页面。如何在struts2中执行此操作。在添加图像操作之前它工作正常。

的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

2 个答案:

答案 0 :(得分:0)

如果您只是想要一个与锚相关联的图像,您可以按如下方式进行:

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

您会注意到我构建了一个命名URL,引用了Struts2锚标记的href属性中的命名URL,并使用内联URL引用了要显示的图像的正确的基于上下文的路径。 / p>

但是,对于您的问题,您</a>中包含的第一个图片链接中似乎有一个迷路products.jsp

答案 1 :(得分:0)

首先,您在 <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> 文件中定义操作并预测该操作类,我们可以使用这种方式调用操作。

<强> Products.jsp

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

和strtus.xml

XML

并定义我在<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>文件中定义的类,并返回Sucess消息并检查.it将确保正常工作。对不起,我已经拍了一些其他图片用于测试目的。谢谢,有任何疑问,请在这里询问。

将strtus.xml中的<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>更改为 var addressWithoutEmtySpacesMoreThanOne = Regex.Replace(address, @"\s+", " "); var result = new Regex("[^a-zA-Zа-яА-Я0-9 -]").Replace(addressWithoutEmtySpaces, ""); customer.Address = result;