struts1 Demo

时间:2021-08-02 04:56:31

每次都会忘记一些东西,反复查找原因,其实struts1很简单,可是不去巩固也很容易忘记并且犯错误。这是一个最简单的登录Demo.

1.建立web工程,引入struts1.2包

2.建package:action和form。

2.1在form下建userform.java

package form;

import org.apache.struts.action.ActionForm;

public class UserForm extends ActionForm {

    /**
* formBean
*/
private static final long serialVersionUID = 1L; private String username;
private String password;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
} }

2.2在LoginAction.java:

如果账户名为”123“,则登录成功。

package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping; import form.UserForm; public class LoginAction extends Action { /*
* (non-Javadoc)
*
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.
* ActionMapping, org.apache.struts.action.ActionForm,
* javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
System.out.println("LoginAction.execute()");
UserForm user = (UserForm) form;
System.out.println("name = " + user.getUsername());
if (user.getUsername().equals("123")) {
return mapping.findForward("success");
} else {
return mapping.findForward("error");
}
} }

3.配置文件:

  3.1在WEB-INF下建立struts-config.xml

(这一步是关键步骤!学会了action的配置基本就掌握了struts1)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="userform" type="form.UserForm"/>
</form-beans>
<action-mappings>
<action
attribute="userform"
path="/login"
name="userform"
scope="request"
type="action.LoginAction"
input="/index.jsp"
>
<forward name="success" path="/WEB-INF/wel.jsp"/>
<forward name="error" path="/WEB-INF/error.jsp"/>
</action>
</action-mappings>
</struts-config>

3.2配置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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<!-- servlet class ?? -->
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

3.3新建jsp页面文件

  index.jsp  (作为input source)

  <form action="login.do" method="post">
name: <input type="text" name="username">
<input type="submit" value="submit">
</form>

跳转页面wel.jsp 和error.jsp

 <body>
This is wel JSP page. <br>
hello <%=request.getParameter("username") %>
</body>