i具有以下用户酶,即在Login
上应显示CustomerForm
,并且应显示从数据库中检索的ListofCustomers
。
我已经在Struts 2中写下了以下代码,但是CustomerAction
中的getCustomerList()
没有被调用,这在struts.xml
Login
操作中被重定向 web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Struts 2 Entry Point is Filter -->
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts2 Application</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>
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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="default" extends="struts-default" namespace="/">
<interceptors>
<interceptor name="mylogging"
class="com.rahul.interceptor.MyLoggingInterceptor">
</interceptor>
<interceptor-stack name="loggingStack">
<interceptor-ref name="mylogging" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<action name="login" class="com.rahul.action.LoginAction">
<interceptor-ref name="loggingStack" />
<result type="redirect">/customer?method=getCustomersList</result>
<result name="success">Welcome.jsp</result>
<result name="error">Login.jsp</result>
</action>
<action name="customer" class="com.rahul.action.CustomerAction">
<interceptor-ref name="loggingStack" />
<result name="success">SuccessCustomer.jsp</result>
<result name="input">Customer.jsp</result>
</action>
</package>
</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>Struts 2 - Login Application</title>
</head>
<body>
<h2>Struts 2 Login Application</h2>
<s:actionerror />
<s:form action="login" method="post">
<s:textfield name="username" key="label.username" size="20" />
<s:password name="password" key="label.password" size="20" />
<s:submit method="execute" key="label.login" align="center" />
</s:form>
</body>
</html>
loginAction.java
:
package com.rahul.action;
public class LoginAction {
private String userName;
private String password;
public String execute() {
if (this.userName.equals("admin") && this.password.equals("admin123")) {
return "success";
} else {
return "error";
}
}
public String getUsername() {
return userName;
}
public void setUsername(String username) {
this.userName = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
CustomerAction.java
:
package com.rahul.action;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class CustomerAction extends ActionSupport {
private String name;
private Integer age;
private String email;
private String telephone;
private List<String> customerList;
private String countryName;
public String getCountryName() {
System.out.println("getCountryName method");
countryName="India";
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String addCustomer() {
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public List<String> getCustomerList() {
return customerList;
}
public void setCustomerList(List<String> customerList) {
this.customerList = customerList;
}
public String getCustomersList() {
System.out.println("Inside Customer List");
customerList = new ArrayList<String>();
customerList.add("Rahul");
customerList.add("Saurabh");
return SUCCESS;
}
}
welcome.jsp
:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>
Howdy,
<s:property value="username" />
...!
</h2>
<%@include file="Customer.jsp"%>
</body>
</html>
Customer.jsp
:
<%@ page contentType="text/html; charset=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=ISO-8859-1">
<title>Customer Page</title>
</head>
<body>
<s:form action="customer.action" method="post" validate="true">
<s:textfield name="name" key="name" size="20" />
<s:textfield name="age" key="age" size="20" />
<s:textfield name="email" key="email" size="20" />
<s:textfield name="telephone" key="telephone" size="20" />
<s:submit method="addCustomer" key="label.add.customer" align="center" />
</s:form>
<s:property value="customerList" />
</body>
</html>
您不能将method
作为参数名称传递。此名称保留用于DMI使用的特殊参数,并具有特殊的语法。首先,如果要使用此参数,并且应该启用它。
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
然后
<result type="redirect">/customer?method:getCustomersList</result>
注意,使用冒号在参数中指定方法名称。
或使用等效语法
<result type="redirect">/customer!getCustomersList</result>
您可能还会想知道:一个结肠安全使用吗?
另外,请勿在Struts标签中使用.action
扩展名
<s:form namespace="/" action="customer" method="post" validate="true">
<s:textfield name="name" key="name" size="20" />
<s:textfield name="age" key="age" size="20" />
<s:textfield name="email" key="email" size="20" />
<s:textfield name="telephone" key="telephone" size="20" />
<s:submit method="addCustomer" key="label.add.customer" align="center" />
</s:form>
注意,Struts标签使用的方法属性将特殊参数添加到形式的URL:method:addCustomer
。此参数仅适用于DMI。如果将命名空间用于操作配置,则应在form
标签中使用它。