Struts入门级配置


   工作之余写了一个关于Struts的基本配置,是一个登陆的流程,一个login.jsp页面,用户名和密码。 没有连接数据库来验证用户登陆信息,而是直接用了一个简单的验证方法。目的是尽量简单化,突出 Struts的配置。这里没有详细介绍Struts的具体参数的意义和使用方法,请参考相关的资料。IDE用的是Idea5。服务器用的是Tomcat5。

请将相关的Struts的包引用进来,具体的文件目录如下:


下面分别将每个文件的具体内容列出,其中有解释:

login.jsp

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ page contentType="text/html; charset=GBK" %>

<html>
<head><title>Login</title></head>
<body><center>
<h1>Login</h1>
<html:form action="/login.do" method="post">
<table>
<tr><td>用户名:<html:text property="name" size="20"/></td></tr>
<tr><td>密 码:<html:password property="password" size="20"/></td></tr>
<tr><td align="center"><html:submit value="Login"></html:submit><html:reset value="Reset"></html:reset> </td></tr>
</table>
</html:form>
</center>
</body>
</html>

 

success.jsp

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ page contentType="text/html; charset=GBK" %>

<html>
<head><title>success</title></head>

<body><h1>登陆成功!</h1>
<html:form action="/login.do" method="post">
<logic:notEmpty name="loginForm" property="name">
<h1>欢迎你:<bean:write name="loginForm" property="name"></bean:write></h1>
</logic:notEmpty>
</html:form>
</body>
</html>

bad.jsp

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ page contentType="text/html; charset=GBK" %>

<html>
<head><title>bad</title></head>

<body><h1>登陆失败!</h1>
<a href="login.jsp"> 重新登陆 </a>
</body>
</html>

web.xml

<?xml version="1.0" encoding="GBK"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<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/conf/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>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<!– The Usual Welcome File List –>

<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>

<!– 申明Struts标签库,taglib是引用路径,taglib-location是本地标签库的路径 –>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>

</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-template</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-template.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
</taglib>

</web-app>

struts-config.xml

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
<!– ========== Form Bean Definitions =================================== –>
<form-beans>
<form-bean name="loginForm" type="form.LoginForm"/>
</form-beans>

<!– ========== Action Mapping Definitions ============================== –>
<action-mappings>
<!– Process a user login –>
<action path="/login" type="action.LoginAction" attribute="loginForm"
name="loginForm"
input="/bad.jsp" scope="request">
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
</struts-config>

 

User.java

package model;

/**
* 这个用户的模型类;
*/

public class User {
private int id;
private String name;
private String password;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

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

UserDao.java

package dao;
import model.User;

/**
* 验证用户登陆信息的类,在实际项目中验证用户信息应该是登陆数据库,查询用户是否存在来决定用户登陆是否登陆成功的,
* 这里主要讲述Struts的基本配置,为了简单就没有配置数据的部分。
*/

public class UserDao {
private static String checkName = "lvxuehu";
private static String checkPassword = "123456";

public static boolean checkUser(User user) {
if (user.getName() != null && user.getPassword() != null) {
if (user.getName().trim().equals(checkName) && user.getPassword().trim().equals(checkPassword)) {
return true;
}
}
return false;
}
}


LoginForm.java

package form;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;

/**
* 这里是Struts的ActionForm Bean部分,Struts将自动将表单数据组装到ActionForm Bean中,这里的属性应该和
* login.jsp页面表单中的控件property 对应,在这个类中有两个方法可以覆盖,validate 和 reset,validate用来
* 负责表单数据的验证,reset可以将数据复位为默认值。
*/

public class LoginForm extends ActionForm {
private int id;
private String name;
private String password;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

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

/**
* 这个方法就是负责验证用户表单的,我在实际项目没有用这个方法来验证表单,而是直接用javaScript在页面上验证,
* 因为ActionEerrors在传输中文的时候容易出现乱码。
*/
// public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
// return super.validate(actionMapping, httpServletRequest); //To change body of overridden methods use File | Settings | File Templates.
// }


public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
this.name = "";
this.password = "";
}
}

LoginAction.java

package action;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import form.LoginForm;
import model.User;
import dao.UserDao;

/**
* Struts 的 Action,负责页面逻辑的跳转;
*/

public class LoginAction extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
LoginForm thisForm = (LoginForm) actionForm;
User user =new User();
user.setName(thisForm.getName());
user.setPassword(thisForm.getPassword());
if(UserDao.checkUser(user)){
return actionMapping.findForward("success");
}

return actionMapping.getInputForward();
}
}


评论

该日志第一篇评论

发表评论

评论也有版权!