spring应用实例



新建一个登陆页面:loginActionSupport.jsp,代码例如以下
 


<%@page contentType="text/html;charset=GBK" isELIgnored="false"%>
 
<html>
 
<head><title>;实现用户登录实例,struts和Spring整合</title></head>
 
<body>


spring
<font size=’22’> $<br> </font> 
<form name="form1" action="/myLogin/loginActionSupportdo" method="post">
 
用户名:<input type="text" name="username" value="${user.username}"/><br>
 
密码:<input type="password" name="password" value="${user.password}"/><br>
 
<input type="submit" name=”method” value="提交"/>
 
</form>
 
</body>
 
</html>
 
创建一个存储登陆用户信息的类:User.java该类继承于ActionForm,代码例如以下:
 


package com.zhaosoft.bean;
 
import org.apache.struts.action.ActionForm;
 
public class User extends ActionForm{
 
private String username=null;
 
private String password=null;
 
public String getUsername() {
 
return username;
 
}
 
public void setUsername(String username) {
 
this.username = username;
 
}
 
public String getPassword() {
 
return password;


spring

public void setPassword(String password) {
 
this.password = password;
 
}
 
}
 
Com.zhaosoft.action中新建一个LoginActionSupport.java,该类不继承于struts的Action,而是继承于Spring的ActionSupport,代码示比例如以下:
 


package com.zhaosoft.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.springframework.context.ApplicationContext;
 
import org.springframework.web.struts.ActionSupport;
 
import com.zhaosoft.bean.User;


spring
import com.zhaosoft.domain.Login; 
public class LoginActionSupport extends ActionSupport {
 
public ActionForward execute(ActionMapping mapping,ActionFormform,
 
HttpServletRequest request,HttpServletResponse response)
 
throws Exception {
 
// 通过ApplicationContext获取配置文件
 
ApplicationContext ctx = getWebApplicationContext();
 
Login login = (Login) ctx.getBean("login");
 
login.login((User) form);


spring
request.setAttribute("msg",login.getMsg()); 
request.setAttribute("user",(User) form);
 
return mapping.findForward("login");
 
}
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值