随笔 - 72  文章 - 28  trackbacks - 0
<2013年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(4)

随笔分类(66)

随笔档案(72)

文章档案(19)

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜

首页要在web.xml中添加相应的struts配置:

复制代码
<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>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
复制代码

然后在config所对应的目录下添加struts配置文件struts-config.xml

复制代码
<?xml version="1.0" encoding="UTF-8" ?>

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

<!-- ActionForm -->
<form-beans>
<!-- name:名称 type:类路径 -->
<form-bean name="loginForm" type="com.login.LoginForm">
</form-bean>
</form-beans>
<!-- 设置全局URL,必须写在Action前面,这样任何Action都可以使用 -->
<global-forwards>
<forward name="result" path="/result.jsp" />
</global-forwards>

<!-- 注册Action -->
<action-mappings>
<!-- name:名称 type:类路径 path:客户端(JSP)提交到服务器端时指定的路径(Form表单中的action=”/login.do" -->
<action path="/login" type="com.login.LoginAction"
name
="loginForm" scope="request">
<!-- 局部URL,只有这个action可以使用 -->
<forward name="success" path="/success.jsp"></forward>
<forward name="error" path="/error.jsp"></forward>
</action>
</action-mappings>


</struts-config>
复制代码
注:Action和Form的关系是一个Action只能对应一个Form(通过Action的name属性)
而一个Form可以被多个Action调用
LoginAction.java
复制代码
package com.login;

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 {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginForm lf
= (LoginForm) form;
String username
= lf.getUsername();
String password
= lf.getPassword();
String errorInfo
= "";
String result
= UserManager.getInstance().login(username, password);
if (result.equals("success")) {
request.setAttribute(
"username", username);
return mapping.findForward("success");
}
else if (result.equals("username")) {
errorInfo
= "密码错误";
}
else {
errorInfo
= "用户名错误";
}
request.setAttribute(
"errorInfo", errorInfo);
return mapping.findForward("error");
}

}
复制代码
LoginForm.java
复制代码
package com.login;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class LoginForm extends ActionForm {

private String username;
private String password;

public void reset(ActionMapping mapping, HttpServletRequest request) {
System.out.println(
"--------reset()-------------");
super.reset(mapping, request);
}

public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
System.out.println(
"--------validate()-------------");
return super.validate(mapping, request);
}

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

}
复制代码
对我个人来说最重要的是前台jsp页面没有掌握,经常在页面上花费很多时间
在编写JSP页面时要用struts标签,而我经常会忘记
引入jsp标签
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>首页</title>
	</head>

	<body>
		<html:form action="/login.do">
			用户名:<html:text property="username"></html:text><br/>
			密码: <html:password property="password"></html:password><br/>
			<html:submit>submit</html:submit>
		</html:form>
	</body>
</html>

 

展示结果(将Action的返回值展示到JSP页面上)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>Error</title>
	</head>
	<body>
		<bean:write name="errorInfo" scope="request" />
	</body>
</html>
posted on 2013-12-27 14:56 kelly 阅读(191) 评论(0)  编辑  收藏 所属分类: java 框架

只有注册用户登录后才能发表评论。


网站导航: