用struts实现简单的登录

1.建项目时选java EE6.0

2.写登陆界面 

<body>
  <center>
    <form id="form1" name="form1" action="action/login.action" method="post">
    用户名<input name="username" type="text"><br>
    密码<input name="password" type="password"><br>
    <input type="submit" value="登录">
    </form>
    </center>
  </body>

3.右击项目名-->Eclipse-->Add Struts Support-->Struts 2.1-->finish

4.建类login.jsp继承ActionSupport

5.写login.java

ackage com.chao.s4;

import com.opensymphony.xwork2.ActionSupport;

public class login extends ActionSupport {

	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 execute() throws Exception {
		// TODO Auto-generated method stub
		String u,p;
		u=getUsername();
		p=getPassword();
		
		if(u.equals("1")&&p.equals("1"))
		{
			return "success";
		}
		else
			return "error";
	}
}

6.写struts.xml(可以拖)

代码:

<struts>

	<package name="default" namespace="/action" extends="struts-default">
		<action name="login" class="com.chao.s4.login">
			<result name="success">../success.jsp</result>
			<result name="error">../error.jsp</result>
		</action>
	</package>
</struts> 

7.最后写success.jsp和error.jsp

<body>
    登陆成功<a href=index.jsp>返回</a><br>
  </body>
<body>
    登陆失败<a href=index.jsp>返回</a><br>
  </body>

 

posted @ 2016-03-22 11:38  非非是  阅读(256)  评论(1编辑  收藏  举报