User
package org.zbq.bean;
public class User {
private String name;
private String pass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Override
public String toString(){
return "Name: " + name + " Password: " + pass;
}
}
LoginService
package org.zbq.service;
import org.zbq.bean.User;
public interface LoginService {
public boolean isLogin(User user);
}
LoginServiceImpl
package org.zbq.service.impl;
import org.zbq.bean.User;
import org.zbq.service.LoginService;
public class LoginServiceImpl implements LoginService {
public boolean isLogin(User user) {
if("admin".equals(user.getName()) &&
"pass".equals(user.getPass())){
return true;
}
return false;
}
}
LoginAction
package org.zbq.struts;
import org.zbq.bean.User;
import org.zbq.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private User user;
private LoginService loginService;
public LoginService getLoginService() {
return loginService;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
if(!loginService.isLogin(user)){
return ERROR;
}
return SUCCESS;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="struts" extends="struts-default">
<action name="loginAction" class="loginAction">
<result name="success">/result.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
</struts>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 无状态的一定要用singleton -->
<bean id="loginService" class="org.zbq.service.impl.LoginServiceImpl" scope="singleton"/>
<!-- 对于action来说 scope一定要设置成 prototype 或是 request -->
<bean id="loginAction" class="org.zbq.struts.LoginAction" scope="prototype">
<property name="loginService" ref="loginService"></property>
</bean>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
</head>
<body>
<form action="loginAction">
<label>Name:</label><input type="text" name="user.name"/> <br/>
<label>Password:</label><input type="text" name="user.pass"/> <br/>
<input type="submit">
<input type="reset">
</form>
</body>
</html>
result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'result.jsp' starting page</title>
</head>
<body>
Welcome ! <br>
Username: <s:property value="user.name"/><br/>
Password: <s:property value="user.pass"/>
</body>
</html>