您的位置:首页 > 其它

SSH登录,注册完整版2(写不下,后面分3,4,......)

2010-02-12 17:27 399 查看
接下来是和实体类对应的form类(由于本程序是struts1.2所有form单独那出来,后续的struts2中将不再单独需要form类).

form类如下:

package com.hp.shangtongwei.form;

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 myform extends ActionForm {

String id;
String name;
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

String password;

public String getId() {
return id;
}

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

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return super.validate(mapping, request);
}

}

接下来是负责页面逻辑和跳转的action类:

如下:

package com.hp.shangtongwei.action;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hp.shangtongwei.dao.persondao;
import com.hp.shangtongwei.form.myform;
import com.hp.shangtongwei.vo.person;

public class personaction extends DispatchAction {

private persondaoImpl persondaot;
public persondaoImpl getPersondaot() {
return persondaot;

public void setPersondaot(persondaoImpl persondaot) {
this.persondaot = persondaot;
}
public ActionForward dologin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

myform mf=(myform)form;
person p=new person();
p.setId(mf.getId());
p.setPassword(mf.getPassword());

boolean flag=false;
try {
flag=persondaot.isLogin(p);
} catch (Exception e) {

e.printStackTrace();
}

if(flag==true){
request.setAttribute("mp", p);
return mapping.findForward("suc");

}

else{
request.setAttribute("mp", p);
return mapping.findForward("fal");

}

}

public ActionForward doinsert(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

myform mf=(myform)form;

person p=new person();

p.setId(mf.getId());
p.setPassword(mf.getPassword());

boolean flag=false;
boolean flagcheck=false;

try {
flagcheck=persondaot.docheck(p);
} catch (Exception e1) {

e1.printStackTrace();
}

System.out.print(flagcheck);

if(flagcheck==false)
{
try {

flag=persondaot.isInsert(p);

request.setAttribute("mp", p);
return mapping.findForward("sucinsert");

} catch (Exception e) {

e.printStackTrace();
}

}
return mapping.findForward("falinsert");
}

}
另外本程序进行了国际化,分别建立了resources_en_US.properties,resources_zh_CN.resources及其基类resources.properties.

负责改变语言的action类为:

package com.hp.shangtongwei.action;

import java.util.Locale;

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 changlanguageaction extends Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//Locale l=null;
// String language=request.getParameter("sellanguage");

// System.out.println("dd"+language);

// if(language.equals("english")){
// l=new Locale("en","US");
// Locale.setDefault(l);

return mapping.findForward("change");
// }

// else{
// l=new Locale("zh","CN");
//Locale.setDefault(l);

//return mapping.findForward("change");
// }

}

}
这个action不带form.

接下来在src下建立struts-config.xml文件和applicationContext(可以为其他的名称:)

建立后要在web.xml中添加对应的信息,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>opensession</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>opensession</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<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>
struts-config.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>

<form-beans>
<form-bean name="personform" type="com.hp.shangtongwei.form.myform"></form-bean>

</form-beans>

<action-mappings>

<action attribute="personform"
name="personform"
parameter="status"
path="/person"
scope="request"
type="com.hp.shangtongwei.action.personaction">
<set-property property="cancellable" value="true" />
<forward name="suc" path="/success.jsp"/>
<forward name="fal" path="/failure.jsp" />
<forward name="sucinsert" path="/successinsert.jsp"/>
<forward name="falinsert" path="/failureinsert.jsp" />
</action>
<action path="/sell" scope="request" type="com.hp.shangtongwei.action.changlanguageaction">
<forward name="change" path="/index.jsp"></forward>

</action>

</action-mappings>
<controller
processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
</controller>//此处来加载applicationContext.xml把struts的跳转交给spring来管理
<message-resources parameter="com.hp.shangtongwei.action.resources">

</message-resources>

//此处用<message-resources patameter=""/>来标识资源文件的位置

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>
//此处插件来加载applicationContext.xml把struts的跳转交给spring来管理

</struts-config>

接下来是applicationContext.xml文件,如下:

待续
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: