集成Struts示例
这里是一个用Struts集成的登录用户的示例
1)在WEB-INF/lib/目录中放入struts.jar
2)在web.xml中设置servlet mapping
QUOTE:
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
3)模块中的jsp
QUOTE:
<%@ taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html"%>
...
<html:form action="/Login" method="post">
Username: <html:text property="name"/>
Password <html:text property="password"/>
<!-- this hidden-fields are important to tell your action about a target after login. Please create a welcome.html and error.html inthe same folder-->
<html:hidden property="errorUri" value="<cms:link><cms:info property="opencms.request.url" />error.html</cms:link>">
<html:hidden property="welcomeUri" value="<cms:link><cms:info property="opencms.request.url" />welcome.html</cms:link>">
</html:form>
...
4)Action
在该action中读取表单,登录用户
QUOTE:
public class LoginAction extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm loginActionForm = (LoginActionForm) actionForm;
try {
String username = loginActionForm.getUsername();
String password = loginActionForm.getPassword();
logger.debug("Login user '" + username);
OpenCms.getAuthorizationHandler().initCmsObject(request, username, password);
} catch (CmsException e) {
logger.error("Can' t login user " + username + ": " + e, e);
// Redirect to error-Site
response.sendRedirect(request.getParameter("errorUri"));
}
// Redirect to Success-Site
response.sendRedirect(request.getParameter("welcomeUri"));
}
}
5) ActionForm 有两个String元素,以及getter and setter 方法 (username and password
6)struts-config.xml
QUOTE:
<form-bean name="login" type="com.xy.form.LoginActionForm" />
<action path="/Login" type="com.xy.action.IntegrationAction" name="login" validate="false" scope="request">
</action>
[ 由
青出于蓝 发表在
OpenCms中文网论坛 ]