Struts 2, Error 404 - java

I have tried many things, but not able to solve the issue of Error 404 while I click the submit button of the HR form. I need to get redirected to the output.jsp page as soon as I submit the form of HR. Can anyone help?
Web.Xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>input.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="default" extends="struts-default">
<action name="output" class="com.vcm.login.LoginAction" method="execute">
<result name="success">/output.jsp</result>
<result name="input">/input.jsp</result>
</action>
</package>
</struts>
LoginAction.java
package com.vcm.login;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
private String message;
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;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute(){
if(username.equals("jyotsna")&& password.equals("jyotsna")){
return Action.SUCCESS;
}
else{
addActionError("NOT a VALID user");
return Action.LOGIN;
}
}
public void validate(){
if((username==null) || (username.trim().equals("")))
addFieldError("username","ID cannot be blank");
if(password==null || password.trim().equals(""))
addFieldError("password","password incorrect");
}
}
Input.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>
<table style="align:center">
<tr>
<td width="25%" style="background-color:#33CCFF">
<form action="output">
<p style="color:white"><b><i>HR LOGIN</i></b></p><br>
HR id:<input type="text" name="username" value="" /><br><br>
Psswd:<input type="password" name="password" value="" /><br><br>
<input type="submit" value="login">
</form>
</td>
<td width="70%">
<img src="https://assets.dice.com/external/images/empLogos/6fccd30f19c09f1afee9414a18 5fde3d.jpg" height="100%" width="100%"></td>
<td width="25%" style="background-color:#33CCFF">
<form method="post" action=" ">
<p style="align:center;color:white"><b><i>EMPLOYEE LOGIN</i></b></p><br>
Emp id:<input type="text" name="id" value="" /><br><br>
Psswd :<input type="password" name="psswd" value="" /><br><br>
<input type="submit" value="login">
</form>
</td>
</tr>
</table>
</body>
</html>
Output.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>output</title>
</head>
<body>
<p>
Page of a HR
</p>
</body>
</html>

Use the struts tags.
Load them by adding this at the beginning:
<%# taglib prefix="s" uri="/struts-tags" %>
Use them like this:
<s:form action="output" method="post">
<s:textfield key="username" label="HR id:" />
<s:textfield key="password" label="Psswd:" />
<s:submit type="button">login</s:submit>
</s:form>
Alter the layout by switching/altering the struts ui theme and by using css.

Related

Execution of Struts basic application getting java.lang.NoSuchMethodException

I'm new to struts framework. So seeking some online tutorials and tried to develop a very basic application. But I'm unable to run it properly
after clicking button action method "execute()" was unable to locate
by container.
I am not getting what I'm missing.
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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SampleApplication</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>sample</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>sample</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
index.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>Sample Struts Application</title>
</head>
<body>
<s:form action="invoke">
<s:textfield name="name" label="UserName"></s:textfield><br>
<s:password name="password" label="Password"></s:password><br>
<s:submit value="GO"></s:submit>
</s:form>
</body>
</html>
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="helloworld" extends="struts-default"><action name="invoke" class="com.techm.SampleStruts" method="execute">
<result name="success">/Success.jsp</result>
</action>
</package></struts>
SampleStruts.java
import com.opensymphony.xwork2.ActionSupport;
public class SampleStruts extends ActionSupport{
private static final long serialVersionUID = 1L;
String name, password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute()
{
return "success";
}
}
Jars used are :
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
servlet-api-2.5.jar
struts2-core-2.0.11.jar
StrutsInterceptors.rar
xwork-2.0.4.jar
Stack trace :
java.lang.NoSuchMethodException: com.techm.SampleStruts.execute()
at java.lang.Class.getMethod(Unknown Source)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.getActionMethod(AnnotationValidationInterceptor.java:55)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:41)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:167)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
Finally i found the solution is that , after adding .action to the action value.
it is working fine.
<s:form action="invoke.action">
<s:textfield name="name" label="UserName"></s:textfield><br>
<s:password name="password" label="Password"></s:password><br>
<s:submit value="GO"></s:submit>

Struts2 login error with MySQL

I have created a simple login application for Struts2 but I can't login with a local MySQL database. My code is the following:
login.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
<s:form action="loginprocess">
<s:textfield name="username" label="Username"></s:textfield>
<s:password name="password" label="Password"></s:password>
<s:submit cssStyle="float:left;" value="Login"></s:submit>
</s:form>
</body>
</html>
loginerror.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Login</title>
</head>
<body>
Sorry username or password error!
<jsp:include page="login.jsp"></jsp:include>
</body>
</html>
main.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="/struts-tags" prefix="s" %>
<!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>My Page</title>
</head>
<body>
<h2>Welcome!</h2>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD
Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="loginprocess" class="com.login.struts2.LoginTest">
<result name="success">main.jsp</result>
<result name="error">loginerror.jsp</result>
</action>
</package>
</struts>
LoginDao.java
package com.login.struts2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class LoginDao {
public static boolean validate(String username, String password){
boolean status=false;
Connection conn = null;
try
{
String URL = "jdbc:mysql://localhost:3306/struts_register2";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, "root", "1235");
PreparedStatement ps=conn.prepareStatement("select * from test where username=? AND password=?");
ps.setString(1,username);
ps.setString(2,password);
ResultSet rs = ps.executeQuery();
status = rs.next();
}
catch(Exception e){e.printStackTrace();}
return status;
}
}
LoginTest.java
package com.login.struts2;
public class LoginTest {
private String username,password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return password;
}
public void setUserpass(String password) {
this.password = password;
}
public String execute(){
if(LoginDao.validate(username, password)){
return "success";
}
else{
return "error";
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
If I try to change the query at LoginDao.java, by having OR instead of AND, it redirects me to main.jsp. I can't find a solution.My database has the fields with "test" (VARCHAR(10) for username and password. Any ideas?
The input field for the password has a name password.
<s:password name="password" label="Password"></s:password>
Then the setter should be the same name.
public void setPassword(String password) {
this.password = password;
}
In this way the parameter will be populated and you will not have null value.

How to receive all request parameters in a model in Java Struts2?

I am trying to receive all the request parameters from a form in controller having a modelobj. But the model parameters remains null. I dont want to create all the parameters and their setter/getters of models in my controller class...Plz help
Form :
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="/struts-tags" prefix="s"%>
<!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>Login</title>
</head>
<body>
<div>
<form action="login" method="post">
<label>Email Id</label>
<input type="text" name="emailId">
<label>Password</label>
<input type="password" name="password">
<input type="submit" value="Login">
</form>
</div>
</body>
</html>
Model :
public class UserModel {
String emailId;
String password;
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Controller :
import com.opensymphony.xwork2.ActionSupport;
public class LoginController extends ActionSupport{
UserModel userModelObj = new UserModel();
public UserModel getUserModelObj() {
return userModelObj;
}
public void setUserModelObj(UserModel userModelObj) {
this.userModelObj = userModelObj;
}
public String login()
{
UserModel userModelObj = new UserModel();
System.out.println(userModelObj.emailId);
System.out.println(userModelObj.password);
return "success";
}
}
On console it returns null null
You must use your bean class reference in your form's name attribute like this :
<div>
<form action="login" method="post">
<label>Email Id</label>
<input type="text" name="userModelObj.emailId"/>
<label>Password</label>
<input type="password" name="userModelObj.password"/>
<input type="submit" value="Login">
</form>
</div>
Otherwise use the concept Of ModelDriven interface.

Struts 2 Login Form Problems

I want to create a simple login form in Struts 2 but I'm having problems at seeing the input fields for some reason and after I submit the name of the user doesn't appear.
Here is the code:
Struts redirects to my struts.xml.
my struts.xml
<!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"/>
<!-- devMode equals mode debug information and reload everything for every request -->
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/User" extends="struts-default">
<action name="Login">
<result>Login.jsp</result>
</action>
<action name="DashboardAction" class="action.DashboardAction">
<result name="success">Dashboard.jsp</result>
</action>
</package>
</struts>
The Bean class
DashboardAction.java:
package action;
public class DashboardAction {
private String username;
public String execute(){
return "success";
}
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;
}
}
The JSPs
Login.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<h1>Struts 2 Login Test</h1>
<form action="DashboardAction" id="form1" method="post" autocomplete="off">
<div class="input placeholder">
<s:textfield name="username" label="Utilizador"/>
</div>
<div class="input placeholder">
<s:password name="password" label="Password"/>
</div>
<div class="submit">
<s:submit value="Entrar" method="execute"/>
</div>
</form>
</body>
</html>
Dashboard.jsp:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome User</title>
</head>
<body>
<h1>Hello
<s:property value="username"/>
</h1>
</body>
</html>
Why doesn't this work after I press submit? It's supposed to go to Dashboard.jsp?
Here you go :
<result name="success">/Dashboard.jsp</result>
You need to specify the full-path to the JSPs in your results. Notice the /. You need to do the same to your other result pages including Login.jsp
You should map the form to the action that accept data submitted. Use Struts tags
<%# taglib prefix="s" uri="/struts-tags" %>
<s:form action="Welcome" ...
<s:textfield name="username" ...
<s:password name="unknown" ...
couldn't map the password field because in your action there's not a property. May be if you create it
private String unknown;
public String getUnknown() {
return unknown;
}
public void setUnknown(String unknown) {
this.unknown = unknown;
}
Define property password in your action class like below.
DashboardAction.java
package action;
public class DashboardAction {
private String username;
private String password; // you don't have this line in your action class.
public String execute(){
return "success";
}
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;
}
}

addFieldError on Struts2 not displaying error on the fields

Hi I m very new to struts2. from some books and websites I got this example. In action class it use validate method to check logic. The method is called bcoz from the print statements. But the error is not shown near to the fields. Jst help me
Index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="/struts-tags" prefix="s"%>
<!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>Home</title>
</head>
<body>
<s:form action="LoginAction ">
<s:textfield name="username" label="Username" />
<s:textfield name="password" label="Password" />
<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>
Struts.xml
<package name="default" extends="struts-default">
<action name="LoginAction" class="org.shammu.struts2.actions.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="input">/index.jsp</result>
</action>
<action name="emptypassthrough">
<result>index.jsp</result>
</action>
</package>
LoginAction.java
package org.shammu.struts2.actions;
import org.shammu.struts2.beans.LoginBean;
import com.opensymphony.xwork2.ActionSupport;
#SuppressWarnings("serial")
public class LoginAction extends ActionSupport{
private String username;
private String password;
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;
}
public String execute(){
LoginBean login = new LoginBean();
login.setPassword(password);
login.setUsername(username);
if (getUsername().equals("abcd")) {
return "success";
} else {
return "input";
}
}
#Override
public void validate() {
if (username.length() < 3) {
System.out.print("user err ok");
this.addFieldError(username, "Username Empty Pls provide");
}
if (password.length() < 3) {
System.out.print("Pass err ok");
this.addFieldError(password, "Password Empoty provride one pkls");
}
}
}
I m not considering execute method. I want jst the error msg printed near to fields... Help
To display fielderror you need to add <s:fielderror /> in your jsp.
It Render field errors if they exists. Specific layout depends on the particular theme. The field error strings will be html escaped by default.
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib uri="/struts-tags" prefix="s"%>
<!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>Home</title>
</head>
<body>
<s:form action="LoginAction ">
<s:textfield name="username" label="Username" /><s:fielderror fieldName="username"/>
<s:textfield name="password" label="Password" /><s:fielderror fieldName="password"/>
<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>
You are not correctly using addFieldError method. The first parameter in that method is a name of the field. In your case it should be "username".
addFieldError("username", "Username Empty Pls provide");

Categories