<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title></title>
</head>
<body>
<form name="form1" method="post" action="/testweb/logincheck.do">
<table width="300" border="0" cellspacing="0" cellpadding="0">
<tr align="center">
<td colspan="2">用户登录信息</td>
</tr>
<tr>
<td>用户名</td>
<td>
<input name="username" type="text" id="username" size="12">user
</td>
</tr>
<tr>
<td>用户密码</td>
<td>
<input name="password" type="text" id="password" size="12">123456
</td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="Submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm
{
private static final long serialVersionUID = 1L;
private String username = "";
private String password = "";
/**
* @return Returns the password.
*/
public String getPassword()
{
return password;
}
/**
* @param password The password to set.
*/
public void setPassword(String password)
{
this.password = password;
}
/**
* @return Returns the username.
*/
public String getUsername()
{
return username;
}
/**
* @param username The username to set.
*/
public void setUsername(String username)
{
this.username = username;
}
}
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 com.is.form.LoginForm;
public class LoginAction extends Action
{
private static final long serialVersionUID = 1L;
public ActionForward execute
(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// this line is here for when the input page is upload-utf8.jsp,
// it sets the correct character encoding for the response
String encoding = request.getCharacterEncoding();
if ((encoding != null) &&
(encoding.equalsIgnoreCase("GB2312")))
{
response.setContentType
("text/html; charset=GB2312");
} else {
response.setContentType
("text/html; charset=GBK");
}
try {
if (form instanceof LoginForm)
{
LoginForm theForm = (LoginForm) form;
if(theForm.getUsername().equals("user") &&
theForm.getPassword().equals("123456"))
{
return new ActionForward("/welcome.do?type=true");
}
else {
return new ActionForward("/welcome.do?type=false");
}
}
} catch (Exception e)
{
}
// this shouldn't happen in this example
return null;
}
}
注意这里是直接用ActionForward转向的,你也可以按照struts中提供的空白例程struts-blank.war中的做法进行转向,可以比较一下会有收获的。
同创建index.jsp页面相同,我们创建welcome.jsp页面,均使用默认设置。并编辑其内容如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title></title>
</head>
<body>
<%
String type = request.getParameter("type");
if(type!=null&&type.equals("true")){
out.print("欢迎您的光临!");
}
else{
out.print("对不起,你输入的用户名或者密码错误!");
}
%>
</body>
</html>
增加Struts-config.xml中的配置
添加formbean的配置,在和标签之间加入:
<form-bean
name="loginForm"
type="com.is.form.LoginForm"/>
添加jsp文件的映射,在和标签之间加入:
<action
path="/index"
forward="/index.jsp"/>
<action
path="/welcome"
forward="/welcome.jsp"/>
添加action文件的映射,在和标签之间加入:
<action
path="/logincheck"
type="com.is.action.LoginAction"
name="loginForm"
scope="request"
validate="true"/>
修改后的struts-config.xml大致如下形式:
<global-forwards>
</global-forwards>
<action-mappings>
<action
path="/index"
forward="/index.jsp"/>
<action
path="/welcome"
forward="/welcome.jsp"/>
<action
path="/logincheck"
type="com.is.action.LoginAction"
name="loginForm"
scope="request"
validate="true"/>
</action-mappings>
<controller processorClass=
"org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="MessageResources"/>
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/>
<set-property property="moduleAware" value="true"/>
</plug-in>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"value="/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
运行测试程序
右键点击testweb工程根目录,点击菜单中的Tomcate project->update context definition,将工程部署进tomcat,成功后会提示操作成功。
点击菜单栏中的雄猫图标启动tomcat,然后在IE地址栏中输入http://localhost:8080/testweb/index.do,我们会看到index.jsp的页面内容。