1,在 HeadFirstStruts2chapter02 中,新建类UserAction2
package com.cruise.action;
import com.cruise.model.User;
import com.cruise.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction2 extends ActionSupport{
private UserService userService = new UserService();
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
System.out.println("执行了UserAction方法");
Boolean login = userService.login(user);
if(login){
return SUCCESS;
}else{
return ERROR;
}
}
}
2,新建login2.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 heretitle>
head>
<body>
<form action="user2" method="post">
userName:<input type="text" name="user.userName"/><br>
password:<input type="text" name="user.password"/><br>
<input type="submit" value="提交">
form>
body>
html>
3, 修改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" extends="struts-default">
<action name="hello" class="com.cruise.action.HelloWorldAction">
<result name="success">helloworld.jspresult>
action>
<action name="hello2" class="com.cruise.action.HelloWorldAction2">
<result name="success">helloworld.jspresult>
action>
<action name="user" class="com.cruise.action.UserAction">
<result name="success">success.jspresult>
<result name="error">error.jspresult>
action>
<action name="user2" class="com.cruise.action.UserAction2">
<result name="success">success.jspresult>
<result name="error">error.jspresult>
action>
package>
struts>