I'm getting an Error : "There is no Action mapped for namespace / and action name login."
Here my complete program code is :
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>HibernateLogin</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 - Hello World</title>
</head>
<body>
<h2 class="heading">
<u>Account Sign in</u>
</h2>
<s:if test="hasActionErrors()">
<div>
<s:actionerror />
</div>
</s:if>
<s:form name="frmLogin" action="login">
<s:textfield id="userName" name="userName" label="User Name"></s:textfield>
<s:password name="password" label="Password"></s:password>
<s:submit align="center"></s:submit>
</s:form>
</body>
</html>
Welcome.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Hello, <s:property value="login.getRole()" />...!</h2>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>HibernateLogin</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.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.devMode" value="true" />
<package name="com.HibernateLogin"
namespace="/"
extends="struts-default">
<action name="login" class="com.HibernateLogin.LoginAction" method="checkLogin">
<result name="success">/Welcome.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
</struts>
Login.java
package com.HibernateLogin;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table (name = "TBL_USER")
public class Login implements Serializable{
private
String userName;
private String password;
private String role;
/**
* @return the userName
*/
@Id
@Column (name = "V_USER_NAME")
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the password
*/
@Column (name = "V_PASSWORD")
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the role
*/
@Column (name = "V_ROLE")
public String getRole() {
return role;
}
/**
* @param role the role to set
*/
public void setRole(String role) {
this.role = role;
}
}
LoginAction.java
package com.HibernateLogin;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings("rawtypes")
public abstract class LoginAction extends ActionSupport implements ModelDriven {
private Login login = null;
private static final long serialVersionUID = 1L;
private LoginDAO loginDAO = new LoginDAO();
public String execute(){
return SUCCESS;
}
public String checkLogin() {
if(login.getUserName().equals("") || login.getPassword().equals("")){
addActionError("Please Enter All Values");
return ERROR;
}else {
login = loginDAO.checkLogin(login);
if(null != login.getRole()){
return SUCCESS;
}else {
addActionError("Invalid User ID / PAssword.");
return ERROR;
}
}
}
public Object getmodel(){
login = new Login();
return login;
}
public Login getLogin() {
return login;
}
public void setLogin(Login login) {
this.login = login;
}
}
LoginDAO.java
package com.HibernateLogin;
import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class LoginDAO {
public Login checkLogin(Login login){
Session session = new Configuration().configure().buildSessionFactory().openSession();
Transaction transaction = null;
String userName = null;
String password = null;
userName = login.getUserName();
password = login.getPassword();
String SQL_QUERY = "SELECT login FROM Login login WHERE login.userName = '"+
userName+"' AND login.password = '"+password+"'";
try{
System.out.println(SQL_QUERY);
transaction = session.beginTransaction();
Query query = session.createQuery(SQL_QUERY);
Iterator it = query.iterate();
if(it.hasNext()){
login = (Login)it.next();
}else{
login.setRole(null);
}
}catch(Exception e){
System.out.println(e.getMessage());
}
return login;
}
}
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/mydb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="com.HibernateLogin.Login" />
</session-factory>
</hibernate-configuration>