项目概要
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<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>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
structs-confing.xml
<?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>
<data-sources />
<form-beans>
<form-bean name="loginForm" type="com.ysl.LoginActionForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/user/login" type="com.ysl.LoginAction" name="loginForm"
scope="request" validate="false">
<forward name="success" path="/login_success.jsp"></forward>
<forward name="error" path="/login_error.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
LoginActionForm.java
package com.ysl;
import org.apache.struts.action.ActionForm;
public class LoginActionForm extends ActionForm {
private String username;
private String userpass;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
}
LoginAction.java
package com.ysl;
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;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm laf = (LoginActionForm) form;
String username = laf.getUsername();
String userpass = laf.getUserpass();
// 这里为了示例简单并没有调用业务逻辑层的方法
if (username.equals("admin") && userpass.equals("admin")) { // 把用户名放在request中,
// 登录成功后getAttrbute("username")取出
request.setAttribute("username", username);
request.setAttribute("hehe", "nimei");
return mapping.findForward("success");
} else {
return mapping.findForward("error");
}
}
}
login.jsp
<body>
test struts
<br>
<form action="user/login.do" method="post">
<input type="text" name="username" />
<input type="password" name="userpass" />
<input type="submit" value="提交" />
</form>
</body>
success.jsp
<body>
success
<br>
<%=request.getParameter("username")%>
<%=request.getParameter("userpass")%>
<%=request.getAttribute("hehe")%>
</body>
erro.jsp
<body>
error
<br>
<%=request.getParameter("username")%>
<%=request.getParameter("userpass")%>
</body>