ssh框架学习笔记(1)

初次学习struts,之前做了一个小项目,对于struts的理解是要经过很多项目的历练才能理解的更深入,这里仅仅是实现了一个form表单提交的功能

开发步骤:

1.安装struts jar包

2.编写struts.xml文件

3.编写action

4.编写jsp页面

5.配置web.xml


一:struts配置文件如下,注意配置文件的名字不能乱取,不然会报错,找不到配置文件,一定要取名为struts.xml




/success.jsp/error.jsp                

这里配置了pachage name属性可以为struts或者default或者其他名字,namespace为url里action前面的url名字,取自struts默认的容器
二:然后编写一个登录action,代码如下:

package Action;import com.opensymphony.xwork2.ActionSupport;public class life extends ActionSupport{/*** */private static final long serialVersionUID = 1L;private String username;//用户名private String password;//密码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;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}private String message;//信息public String execute() throws Exception{try{if(username.equals("")||password.equals("")){this.setMessage("用户名或密码为空,请重新输入");return "error";}else if(!username.equals("wulonghui")){this.setMessage("用户名不存在");return "error";}else if(!password.equals("123456")){this.setMessage("密码错误");return "error";}else{this.setMessage("登录成功");return "success";}}catch(Exception e){e.printStackTrace();return "error";}}
}
这里对登录用户名和密码进行了验证

三:编写相应的jsp页面,success.jsp和error.jsp

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
返回成功页面    ${message}


error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'error.jsp' starting page    ${message} 
index.jsp(登录界面显示)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page    <%--form表单提交--%>
用户名:密码:
四:web.xml配置


index.jspstruts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  struts2*.action


ok,整个工程搭建完成




本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部