Struts 2,错误404

时间:2015-06-02 11:26:11

标签: java jsp struts2

我已经尝试了很多东西,但是当我单击HR表单的提交按钮时无法解决错误404的问题。我需要在提交HR表单后立即重定向到output.jsp页面。有人可以帮忙吗?

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>

1 个答案:

答案 0 :(得分:0)

使用struts标签。

通过在开头添加它来加载它们:

<%@ taglib prefix="s" uri="/struts-tags" %>

像这样使用它们:

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

通过切换/更改struts ui主题并使用css来改变布局。