一、新建空项目user和配置maven实现下面的页面
1.配置内容
2.编写struts.xml实现页面
<!--定义一个useraction--> <package name="mypackage" extends="struts-default"> <!--处理登录信息的action--> <action name="userAction_login" class="com.nf.action.User" method="login"> <result name="success">/WEB-INF/jsp/success.jsp</result> <result name="error">/WEB-INF/jsp/error.jsp</result> </action> <!--访问登录页面的action--> <action name="userAction_loginView" class="com.nf.action.User" method="loginView"> <result name="success">/WEB-INF/jsp/loginView.jsp</result> </action> </package>
3.建立User.java
package com.nf.action; import com.opensymphony.xwork2.ActionSupport; public class User extends ActionSupport{ private String userName; private String userPassword; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } //用于处理登录信息,判断成功或者失败 public String login(){ System.out.println("用户名:"+userName); if ("ad".equals(userName)&&"123456".equals(userPassword)){ return this.SUCCESS; }else { return this.ERROR; } } //用于跳转到登录页面 public String loginView(){ return this.SUCCESS; } }
4.建立jsp包,在建三个jsp文件,但文件名必须与struts上的文件名一致
在loginView.jsp设计页面信息
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 用户登录<br> <form action="userAction_login.action"> 用户名:<input type="text" name="userName"> 密码:<input type="password" name="userPassword"> <input type="submit" value="登录"> </form> </body> </html>
在index.jsp里设置跳转
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% response.sendRedirect("userAction_loginView.action"); %>