第06講 struts2的設定資料-模型驅動
1,在 HeadFirstStruts2chapter02 中,新建類UserAction3,實現ModelDriven。ModelDriven的缺點是隻能載入一個bean。
import com.cruise.model.User;
import com.cruise.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction3 extends ActionSupport implements ModelDriven<User>{
private UserService userService = new UserService();
private User user = new User();
@Override
public String execute() throws Exception {
System.out.println("執行了UserAction方法");
Boolean login = userService.login(user);
if(login){
return SUCCESS;
}else{
return ERROR;
}
}
@Override
public User getModel() {
return user;
}
}
2,修改struts.xml,
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="suibiandingyi"
<action name="hello" class="com.cruise.action.HelloWorldAction">
<result name="success">helloworld.jsp</result>
</action>
<action name="hello2"
<result name="success">helloworld.jsp</result>
</action>
<action name="user" class="com.cruise.action.UserAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
<action name="user2" class="com.cruise.action.UserAction2">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
<action name="user3" class="com.cruise.action.UserAction3">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
3,新建login3.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>
<form action="user3" method="post">
userName:<input type="text" name="userName"/><br>
password:<input type="text" name="password"/><br>
<input type="submit" value="提交">
</form>
</body>
</html>
4,測試,
瀏覽器位址列輸入:http://localhost:8080/HeadFirstStruts2chapter02/login3.jsp測試